Java xPath文本包含Selenium Web驱动程序
我正在尝试根据其文本内容选择一个元素。我正在使用XPath来实现这一点 我只是感到困惑,这应该有效吗Java xPath文本包含Selenium Web驱动程序,java,selenium,xpath,Java,Selenium,Xpath,我正在尝试根据其文本内容选择一个元素。我正在使用XPath来实现这一点 我只是感到困惑,这应该有效吗 WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]")); 我甚至会复制HTML代码: <div class="linkWrap noCount">Notifications <spa
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
我甚至会复制HTML代码:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
通知(0)
div元素中有单词“Notifications”。那它为什么不起作用呢
转到Facebook上的此页面:
使用此选项可以通过xPath突出显示任何区域。在单词Notifications之前有一个空格:
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
在尝试查找元素之前,还应添加等待元素:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(), 'Notifications')]"));
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
我是在社区里一些了不起的人的帮助下发现这个问题的 好的,我的元素在一个iFrame中 为了访问元素,我必须首先访问iFrame
WebElement iframe = obj.driver.findElement(By.xpath("//iframe[@tabindex='-1']"));
obj.driver.switchTo().frame(iframe);
哎呀!那不应该在那里。当我测试它的时候,我很肯定我没有在里面。我在没有空间的情况下试过。它似乎仍然不起作用。请听我问题的最后一部分。我在Facebook上引用的确切位置也是。然后使用chrome扩展通过XPath高亮显示任何区域。当我将代码复制到Stackoverflow时,在该点有一个换行符。我可能是有意的。但是我测试的代码没有这样的空间,您不需要chrome扩展来测试xpath。开发工具做得很好。是错误:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:”//div[contains(text(),'Notifications')]”