【发布时间】:2020-10-29 18:24:27
【问题描述】:
我正在尝试引用并单击网页上的元素。
这是网页:https://www.facebook.com/settings
这是元素:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
这是一个包含“通知”一词的 DIV
我们应该能够使用简单的 xPath 进行引用。如//div[contains(text(), 'Notifications')]
但是,它似乎不起作用。
我使用的chrome extension 说该元素不存在。
我的代码找不到元素。
这是我使用 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 不存在的任何想法。
【问题讨论】:
-
这能回答你的问题吗? xPath Text Contains Selenium Web Driver
-
Facebook 不允许抓取。这违反了他们的服务条款。
-
JeffC 我不是在刮擦或类似的东西。我只是将一些非常常规的简单任务自动化。
-
DMart 我在下面有一个答案。这是我遇到这种行为的一个非常合理的原因。我会测试一下
-
DMart 下面的答案是正确答案。