我无法使用driver.findElement(By.xpath())找到带有xpath的Web元素. [英] I can't find Web element with xpath using driver.findElement(By.xpath());

查看:559
本文介绍了我无法使用driver.findElement(By.xpath())找到带有xpath的Web元素.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在页面上找到链接以单击:

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代码:

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

推荐答案

感谢您提供所有答案.我找到了解决问题的办法.最后一个命令是与IFrame链接的命令

Thank you for all answers. I found solution to my problem. The last command was command which was linked with 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"); 

因此我之所以进入IFrame实际上是因为我找不到任何元素. 我执行了以下命令:

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

 driver.switchTo().defaultContent();

之后,可以找到定位器.

After that it is possible to find locators.

这篇关于我无法使用driver.findElement(By.xpath())找到带有xpath的Web元素.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆