Java 我无法使用 driver.findElement(By.xpath()) 找到带有 xpath 的 Web 元素;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18531811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:49:27  来源:igfitidea点击:

I can't find Web element with xpath using driver.findElement(By.xpath());

javaseleniumselenium-webdriver

提问by Bilow Yuriy

I tried to find the link on a page to click:

我试图在页面上找到要点击的链接:

<a id="folder0" class="js-folder icon-wrap icon-wrap_left menu__item__link menu__item__link_act menu__item__link_unread" href="/messages/inbox" rel="history">
    <span class="js-folder-b-unread js-folder-unread menu__item__link__qnt">7</span>
    <i class="js-folder-ico icon icon_left icon_folders icon_inbox_act"></i>
    <span class="menu__item__link__text menu__item__link__text_linear">Input</span>
</a>

Java code:

爪哇代码:

driver.findElement(By.xpath(".//*[@id='folder0']/span[2]")).click();

But the driver can't locate the element:

但是驱动程序找不到元素:

 org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='folder0']/span[2]"}

采纳答案by Bilow Yuriy

Thank you for all answers. I found solution to my problem. The last command was command which was linked with IFrame

谢谢大家的回答。我找到了解决我的问题的方法。最后一个命令是与 IFrame 链接的命令

WebElement editorFrame = driver.findElement(By.cssSelector("#sentmsgcomposeEditor_ifr"));
   driver.switchTo().frame(editorFrame);

   WebElement body1 = driver.findElement(By.tagName("body"));
   body1.sendKeys(Keys.CONTROL + "a"); 

So i was in IFrame actually because of it i couldn't find any element. I performed following command:

所以我实际上是在 IFrame 中,因为我找不到任何元素。我执行了以下命令:

 driver.switchTo().defaultContent();

After that it is possible to find locators.

之后就可以找到定位器了。

回答by LaurentG

If Selenium gives you this error, it simply means that the XPath can't find any element. Obviously, your XPath expression is wrong.

如果 Selenium 给你这个错误,它只是意味着 XPath 找不到任何元素。显然,您的 XPath 表达式是错误的。

In the given XPath expression, you are looking for the second spanelement directly under any element with id= folder0. Your given HTML code doesn't see any identifier on the parent element of the spanyou are trying to find.

在给定的 XPath 表达式中,您正在查找span任何带有id= 的元素正下方的第二个元素folder0。您给定的 HTML 代码在span您尝试查找的父元素上看不到任何标识符。

It is hard to say what would be the correct XPath expression without seeing the whole page.

如果没有看到整个页面,很难说出正确的 XPath 表达式是什么。

回答by James Dunn

You should try to locate by id instead of by xpath.

您应该尝试通过 id 而不是 xpath 定位。

driver.findElement(By.id("folder0")).click();

Two reasons:

两个原因:

  1. Locating by id is usually faster (see herefor why).
  2. Since you're trying to test the link, you don't need to click on an inner <span>, just on the link element itself.
  1. 通过 id 定位通常更快(请参阅此处了解原因)。
  2. 由于您正在尝试测试链接,因此您无需单击内部<span>,只需单击链接元素本身即可。

If you still want to use xpath, or still want to get the inner <span>, you can use firebug in Firefox to copy the xpath; this will give you the correct xpath and show you if you made a mistake.

Firebug Copying XPath

如果你还想使用 xpath,或者还想得到内部<span>,你可以在 Firefox 中使用 firebug 来复制 xpath ;这将为您提供正确的 xpath 并显示您是否犯了错误。

Firebug 复制 XPath

回答by Dingredient

Try it without the .

不带就试试 .

driver.findElement(By.xpath("//*[@id='folder0']/span[2]")).click();