WebDriver 无法使用 Java 通过 xpath 找到元素
以下是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的 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: 的时间
,或者您可以使用显式等待特定元素,如下所示:
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:
or you can use explicit wait for the specific element as below:
由于您想要波兰语 Google 网站,因此该网站
不适合您。而是使用
Since you want the Polish Google site the
will not work for you. Instead use
@用户729076:
您为谷歌文本字段编写的 xpath "//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input" 不正确。 google 文本字段的 HTML 如下:
基于上面的 HTML,您可以简单地使用 id 或 xpath,如下所示:
通过 id:
通过 xpath:
@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:
Based on the above HTML, you can use simply id or xpath as below:
By id:
By xpath:
在这种情况下,您想要的 XPath 表达式是:
或者您可以使用这个(更直观一点):
请记住,如果您将通过 XPath 识别大量元素,那么建议您精通 XPath,您可以开始此处:Xpath 教程
同时,使用 Firefox 并安装以下插件:
Firebug
FirePath 或 Firefinder
这些将帮助您轻松识别有效的 XPath 表达式用于您的网站。
In this case the XPath expression you want is:
Or you could use this (a little more intuitive):
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.
如果你想通过 XPath 查找元素。然后执行以下操作:
另请在此网站上找到更多帮助
If you want to find elements by XPath. Then do the following:
Also find more help on this site
这里使用的 XPath 不正确,直接 Id 就在那里,所以不需要使用 XPath。
The XPath Used is Incorrect Here Directly Id is there so no need to use XPath.