当前位置: 首页 > 知识库问答 >
问题:

无法定位元素xPath Selenium Webdriver

洪彬
2023-03-14

我试图引用并单击网页上的元素。

<div class="linkWrap noCount">Notifications&nbsp;<span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>

我们应该能够使用简单的XPath进行引用。例如//div[contains(text(),'notifications')]

然而,这似乎并不奏效。

我使用的chrome扩展表示该元素不存在。

我的代码找不到元素。

下面是我使用Selenium web驱动程序的Java代码。

// Navigate to page
obj.driver.get("https://www.facebook.com/settings");        

// Write to console where we are
System.out.println(obj.driver.getCurrentUrl());

// Wait | Plenty of time for the page to load
Thread.sleep(5000);     

obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]")).click();

以下是错误:

太奇怪了!关于为什么我不能引用元素,或者为什么xPath不exist的任何想法。

共有1个答案

巫英纵
2023-03-14

您的元素位于iframe中。

对于selenium,您需要切换帧以便访问其中的元素

我还没有为您的站点尝试过它-但您可以使用:

driver.switchTo().frame(1); // by index
//<or> 
driver.switchTo().frame("id of the element"); //by id
//<or> 
driver.switchTo().frame(element); // be element

然后,准备就绪后,切换回主页面/框架:

  driver.switchTo().defaultContent();

在这里查看selenium文档的框架部分

 类似资料:
  • 我想在selenium中找到我的文本字段,但我不知道如何找到(我第一次使用sellenium)。 我试过: 或者通过xPath和cssSseltor String在dev工具中由chrome生成。 请帮助我,我将感谢解释。 这是html:

  • 我试图从一个网站获得一些数据,但得到以下错误。它昨晚起作用了,但当我今天重新运行时,它突然无法定位元素。今天,我尝试了几乎我可以,但无法解决它。 工具和语言-Python、Selenium、Chrome、Chromedriver、AWS Cloud 9、EC2 错误消息 我试过下面的东西 增加和减少睡眠时间。增加和减少睡眠时间 提到各种网站仍然无法解决。我是python新手。

  • 嗨,我是一个相当新的硒。可以有人请建议如何定位和元素在iframe asi是得到错误下面。 带有Iframe标记的DOM按钮元素的屏幕截图 以前编写的代码

  • 问题内容: 我正在尝试搜寻房地产网站上的商品。它有一个aspx表单,必须在提交之前填写。 http://www.cbre.us/PropertyListings/Pages/Properties-for- Sale.aspx 我只关心俄勒冈州的多户家庭财产。所以这是我的第一次尝试: 当我运行此脚本时,出现错误“找不到元素“ ForSalePropertyType”。在这里我在做什么错? 问题答案:

  • 这就是我得到的错误

  • 我正在尝试点击图中所示的下拉菜单 这就是我尝试过的 但它给了我这个错误 NoSuchElementException: Message:找不到element://div[@ class = ' choosed-container choosed-container-single ']//a[@ class = ' choosed-single choosed-single-with-deselec