WebDriver 无法使用 Java 通过 xpath 找到元素

发布于 2024-11-03 16:28:43 字数 1770 浏览 1 评论 0原文

以下是使用 Java 的 WebDriver 代码片段:

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.pl/");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
        WebElement element = driver.findElement(By.name("q")); 
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement query = driver.findElement(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input"));

query.sendKeys("asd");

执行代码后,出现以下异常:

线程“main”中的异常org.openqa.selenium.NoSuchElementException:无法定位元素:{“method”:“xpath”,“selector”:“//html/body/div[2]/span/center /form/table/tbody/tr/td[2]/div/div/input"} 系统信息:os.name:'Windows 7',os.arch:'x86',os.version:'6.1',java.version:'1.6.0_24' 驱动程序信息:driver.version:RemoteWebDriver 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(来源未知) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(来源未知) 在 java.lang.reflect.Constructor.newInstance(来源未知) 在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:409) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:192) 在 org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:265) 在 org.openqa.selenium.By$6.findElement(By.java:205) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:184) 在 test.main(test.java:24)

我的代码有什么问题?

The following is the snippet of WebDriver code using Java:

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.pl/");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
        WebElement element = driver.findElement(By.name("q")); 
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement query = driver.findElement(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input"));

query.sendKeys("asd");

After execution of code I got the following exception:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input"}
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_24'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:409)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:192)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:265)
at org.openqa.selenium.By$6.findElement(By.java:205)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:184)
at test.main(test.java:24)

What's the wrong in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

裸钻 2024-11-10 16:28:43

您的 xpath 表达式:

WebElement query = driver.findElement(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div /input"));

看起来正确,但如果您仍然遇到问题,请再次检查 xpath 的正确性。如果再次失败,请增加 Wait as: 的时间

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

,或者您可以使用显式等待特定元素,如下所示:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input")));

Your xpath expression:

WebElement query = driver.findElement(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input"));

looks correct but if you still are facing the issue please check the correctness of xpath again. If it fails again increase the time for Wait as:

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

or you can use explicit wait for the specific element as below:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input")));
清风夜微凉 2024-11-10 16:28:43

由于您想要波兰语 Google 网站,因此该网站

    //input[@title='Google Search']

不适合您。而是使用

    //input[@title='Szukaj w Google']

Since you want the Polish Google site the

    //input[@title='Google Search']

will not work for you. Instead use

    //input[@title='Szukaj w Google']
岁月无声 2024-11-10 16:28:43

@用户729076:
您为谷歌文本字段编写的 xpath "//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input" 不正确。 google 文本字段的 HTML 如下:

<input type="text" value="" autocomplete="off" name="q" class="gbqfif" id="gbqfq" style="border: medium none; padding: 0pt; margin: 0pt; height: auto; width: 100%; background: url("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D") repeat scroll 0% 0% transparent; position: absolute; z-index: 6; left: 0px; outline: medium none;" dir="ltr" spellcheck="false">

基于上面的 HTML,您可以简单地使用 id 或 xpath,如下所示:
通过 id:

driver.findElement(By.id("gbqfq")).sendKeys("some text");

通过 xpath:

driver.findElement(By.xpath("//input[@id='gbqfq']")).sendKeys("some text");

@user729076:
The xpath "//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input" you wrote for google text field is not right. The HTML for google text field is as follows:

<input type="text" value="" autocomplete="off" name="q" class="gbqfif" id="gbqfq" style="border: medium none; padding: 0pt; margin: 0pt; height: auto; width: 100%; background: url("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D") repeat scroll 0% 0% transparent; position: absolute; z-index: 6; left: 0px; outline: medium none;" dir="ltr" spellcheck="false">

Based on the above HTML, you can use simply id or xpath as below:
By id:

driver.findElement(By.id("gbqfq")).sendKeys("some text");

By xpath:

driver.findElement(By.xpath("//input[@id='gbqfq']")).sendKeys("some text");
趁年轻赶紧闹 2024-11-10 16:28:43

在这种情况下,您想要的 XPath 表达式是:

//html/body/center/form/table/tbody/tr/td[2]/div/input

或者您可以使用这个(更直观一点):

//input[@title='Google Search']

请记住,如果您将通过 XPath 识别大量元素,那么建议您精通 XPath,您可以开始此处:Xpath 教程

同时,使用 Firefox 并安装以下插件:

Firebug

FirePathFirefinder

这些将帮助您轻松识别有效的 XPath 表达式用于您的网站。

In this case the XPath expression you want is:

//html/body/center/form/table/tbody/tr/td[2]/div/input

Or you could use this (a little more intuitive):

//input[@title='Google Search']

Keep in mind that if you will be identifying a lot of elements by XPath it would be advisable to become fluent in XPath, you could start here: Xpath Tutorial

In the meantime, use Firefox and install the following plugins:

Firebug

FirePath or Firefinder

These will help you easily identify valid XPath expressions to use for your website.

独守阴晴ぅ圆缺 2024-11-10 16:28:43

如果你想通过 XPath 查找元素。然后执行以下操作:

WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.google.com";
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("http://www.google.com");
selenium.isElementPresent(XPath Variable);

另请在此网站上找到更多帮助

If you want to find elements by XPath. Then do the following:

WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.google.com";
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("http://www.google.com");
selenium.isElementPresent(XPath Variable);

Also find more help on this site

够钟 2024-11-10 16:28:43

这里使用的 XPath 不正确,直接 Id 就在那里,所以不需要使用 XPath。

driver.findElement(By.id("gbqfq")).sendKeys("xyz");

The XPath Used is Incorrect Here Directly Id is there so no need to use XPath.

driver.findElement(By.id("gbqfq")).sendKeys("xyz");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文