【问题标题】:Selenium(WebDriver) Cannot find and click element: <a class="page-next" href="javascript:gotoPage(2)"></a>Selenium(WebDriver) 找不到并单击元素:<a class="page-next" href="javascript:gotoPage(2)"></a>
【发布时间】:2021-08-17 12:08:10
【问题描述】:

-您好,感谢您阅读我的帖子。我花了一整天的时间试图弄清楚这一点。只需尝试单击上述元素以迭代到产品评论的下一页(这不会更改 URL)。该元素对应于页面产品反馈部分的“下一步”按钮。

这是我的代码:

using OpenQA;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.IE;

....

IWebDriver driver = new FirefoxDriver();

driver.Navigate().GoToUrl("http://www.aliexpress.com/item/Wholesale-DIY-Cardboard-Hang-tag-Retro-Gift-Hang-tag-500pcs-lot-Free-shipping-Thank-you/538222647.html");

driver.FindElement(By.XPath("/html/body/div[3]/div[10]/div[2]/div[2]/div/div/ul/li[2]/a")).Click();

上面的点击命令只是打开网页的产品反馈部分(有效)。接下来我只想选择产品反馈的下一页但没有成功。以下是我尝试单击下一步按钮并移动到产品反馈列表的下一页的一些命令。正如您可能看到的那样,我是编程新手。

driver.FindElement(By.XPath("/html/body/div/div[2]/div/div[2]/div/a[3]")).Click();
NoSuchElementException was unhandled: Unable to locate element: "method":"xpath","selector":"/html/body/div/div[2]/div/div[2]/div/a[3]"}"

driver.FindElement(By.XPath("//a[contains(@class,'page-next')]")).Click();
NoSuchElementException was unhandled:Unable to locate element: {"method":"xpath","selector":"//a[contains(@class,'page-next')]"}

       driver.FindElement(By.XPath("/html/body/div/div[2]/div/div[2]/div/a[@href='javascript:gotoPage(2)]")).Click(); 
InvalidSelectorException was unhandled: The given selector /html/body/div/div[2]/div/div[2]/div/a[@href='javascript:gotoPage(2)] is either invalid or does not result in a WebElement. The following error occurred:

InvalidSelectorError: Unable to locate an element with the xpath expression /html/body/div/div[2]/div/div[2]/div/a[@href='javascript:gotoPage(2)] because of the following error:

[Exception... "The expression is not a legal expression."  code: "12" nsresult: "0x805b0033 (SyntaxError)"  location: "file:///C:/Users/Danny/AppData/Local/Temp/anonymous695678260.webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js Line: 5773"]

    driver.FindElement(By.XPath("//a[@href='javascript:gotoPage(2)]")).Click(); 
InvalidSelectorException was unhandled: The given selector //a[@href='javascript:gotoPage(2)] is either invalid or does not result in a WebElement. The following error occurred:

InvalidSelectorError: Unable to locate an element with the xpath expression //a[@href='javascript:gotoPage(2)] because of the following error:  

[Exception... "The expression is not a legal expression."  code: "12" nsresult: "0x805b0033 (SyntaxError)"  location: "file:///C:/Users/Danny/AppData/Local/Temp/anonymous2137829175.webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js Line: 5773"]         

driver.FindElement(By.LinkText("2")).Click();

No error在这里,但是这段代码点击了页面上的错误按钮。

driver.FindElement(By.LinkText("javascript:gotoPage(2)")).Click();   
NoSuchElementException was unhandled: Unable to locate element: {"method":"link text","selector":"javascript:gotoPage(2)"}

driver.FindElement(By.TagName("a")).FindElement(By.LinkText("Next")).Click();
NoSuchElementException was unhandled: Unable to locate element: {"method":"link text","selector":"Next"}

driver.FindElement(By.CssSelector("html body.product-evaluation div#transction-feedback div.rating-detail div.topnav div#pagination-top.pagination div.pos-right a.page-next")).Click();
NoSuchElementException was unhandled: Unable to locate element: {"method":"css selector","selector":"html body.product-evaluation div#transction-feedback div.rating-detail div.topnav div#pagination-top.pagination div.pos-right a.page-next"}

IJavaScriptExecutor JavascriptExecutor = driver as IJavaScriptExecutor;

JavascriptExecutor.ExecuteScript("gotoPage(2)");
"Unexpected error. ReferenceError: gotoPage is not defined"

driver.FindElement(By.XPath("//a[contains(text(),'Next')]")).Click();

没有错误,没有任何反应

driver.FindElement(By.CssSelector("css=a.page-next")).Click(); 

InvalidSelectorException was unhandeled:The given selector css=a.page-next is either invalid or does not result in a WebElement. The following error occurred:[Exception... "An invalid or illegal string was specified"  code: "12" nsresult: "0x8053000c (NS_ERROR_DOM_SYNTAX_ERR)"  location: "file:///C:/Users/Danny/AppData/Local/Temp/anonymous797491401.webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js Line: 7717"]

driver.FindElement(By.XPath("//div[@id='pagination-top']/div/a[2]"));

NoSuchElementException was unhandled:Unable to locate element: {"method":"xpath","selector":"//div[@id='pagination-top']/div/a[2]"}

我也尝试过:Internet Explorer 中的自动化,并等待页面完全加载后再继续。

【问题讨论】:

  • 您是否每次都遇到“ElementNotFound”类型的异常?如果不是,您收到的异常/错误/失败状态是什么?
  • 你好,梨花山。我会将每个错误放在代码下方
  • @Danny 你确定下一页按钮不在框架内吗?
  • 嗨,HemChe。是的,它在一个框架内。我不知道框架是什么,所以我没有想到这可能是问题的一部分。现在我看到有很多关于访问框架内元素的信息,所以我会研究一下,希望能找到解决方案,谢谢。
  • @HemChe 该元素似乎在 iframe 中。我找不到框架的名称或 ID,所以我尝试使用索引切换 IWebDriver,例如:driver.SwitchTo().Frame(0); driver.SwitchTo().Frame(1); ...(最多 20 个)原来唯一没有抛出 NoSuchFrameException 的索引是 0。这是否意味着只有一帧?

标签: c# javascript xpath selenium-webdriver


【解决方案1】:

以下是我关注的情况:

driver.FindElement(By.XPath("//a[contains(@class,'page-next')]")).Click();

NoSuchElementException 未处理:无法定位元素: {"method":"xpath","selector":"//a[contains(@class,'page-next')]"}

我建议您必须在搜索元素之前添加一个方法,以等待它在页面上的存在。我觉得您正在搜索的元素在您尝试搜索时尚未加载到页面上,因此 Selenium 立即失败。我建议去this link 并使用隐式或显式等待。我觉得这是问题所在,因为根据标题中的 HTML,您的 XPath 看起来是正确的。

它们之间的区别在于,每次搜索元素时都会设置隐式等待,而显式等待可以有任何等待条件,并且每种情况可以有不同的超时时间。阅读提供的链接,看看是否有意义,如果没有,请发布您的尝试,我会尽力提供帮助。

【讨论】:

  • 再次您好 Nashibukasan。感谢您的回复并解释了隐式等待和显式等待之间的区别。我还没有将方法添加到我的代码中,但是我在代码中放置了一个断点,就在总是失败的代码行之前。我一直等到我可以在浏览器中看到下一个按钮,然后在 Visual Studio 中单击继续。不幸的是,我仍然收到相同的错误消息。
  • 我刚刚注意到您不一定需要 xpath 的“包含”部分。事实上,您可能根本不必使用 XPath!试试这个:driver.FindElement(By.ClassName("page-next")).Click();
  • 谢谢 Nashibukasan。我尝试了 FindElement 命令,但它不起作用。在加载新框架并等待页面加载很长时间后,我确实使用 FindElement 命令来单击,但之后它会再次工作,所以它似乎是其他一些随机因素。
  • 作为后续行动,我注意到当去另一个互联网连接更好的地方时,问题就消失了(当然,前提是我使用了适当的等待和帧切换)
  • 如果更好(更快)的 Internet 连接会影响结果,这一定是时间问题。继续尝试,直到得到结果。
【解决方案2】:

我今天使用以下 xpath 遇到了这个问题: //a[@class='myclass' and Text()='My Special link']

我应该使用 xpath 工具验证 firebug 中的语法,它找到了元素。在 java 代码中,它抛出了和你一样的异常。我发现错误的事情是“Text()”,我将其更改为“text()”并且它有效。微妙的区分大小写的问题哈哈。

我还在另一个论坛看到了类似的语法问题,例如: //[@class='myclass' and Text()='My Special link']

我猜这个人想找到“myclass”类的任何东西,它没有用。但是,当添加 * 时它起作用了。 //*[@class='myclass' and Text()='My Special link']

看看你的 xpath 的语法,

干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 2016-07-02
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多