Selenium(WebDriver)无法找到并单击元素:

本文关键字:quot class page-next 元素 WebDriver 单击 Selenium | 更新日期: 2023-09-27 18:10:09

-您好,感谢您阅读我的帖子。我花了一整天想弄明白这件事。只是尝试点击上面的元素来迭代到产品评论的下一页(这不会改变URL)。该元素对应于页面产品反馈部分的"next"按钮。

下面是我的代码:

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();

No error, nothing happened

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]"}

我也尝试过在ie中自动加载,等待页面完全加载后再继续。

Selenium(WebDriver)无法找到并单击元素:<a class="page-next"h

以下是我关注的情况:

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

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

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

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

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

我应该使用xpath工具验证firebug中的语法,它找到了元素。在java代码中,它抛出了与您完全相同的异常。我发现出错的是"Text()"我将其更改为"text()",它可以工作。微妙的大小写敏感问题lol。

我也在另一个论坛看到了类似的语法问题,比如://[@class ="myclass"和文本()= '我的特殊链接']

我猜这个人想找到任何类"myclass",它没有工作。不过,当添加一个*时,它就可以工作了。//* [@class ="myclass"和文本()= '我的特殊链接']

看一下xpath的语法,

干杯!