xPath文本包含Selenium Web Driver

huangapple go评论141阅读模式
英文:

xPath Text Contains Selenium Web Driver

问题

我正在尝试根据元素的文本内容选择元素。我正在使用XPath来实现这一点。

我感到困惑,因为这应该有效?

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

我甚至会复制HTML代码:

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

div元素内部有文字“Notifications”。那么为什么它不起作用呢?

访问Facebook上的此页面:https://www.facebook.com/settings

使用此Chrome扩展通过xPath突出显示任何区域。

英文:

I'm trying to select an element based on its text contents. I am using XPath to achieve this.

I am just puzzled as this should work?

WebElement link = obj.driver.findElement(By.xpath(&quot;//div[contains(text(), &#39;Notifications&#39;)]&quot;));	

xPath文本包含Selenium Web Driver

I'll even copy the HTML code:

&lt;div class=&quot;linkWrap noCount&quot;&gt;Notifications&amp;nbsp;&lt;span class=&quot;count _5wk0 hidden_elem uiSideNavCountText&quot;&gt;(&lt;span class=&quot;countValue fsm&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;maxCountIndicator&quot;&gt;&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;

The div element has the words "Notifications" inside it. So why doesn't it work.

Go to this page on Facebook: https://www.facebook.com/settings

Use this chrome extension to highlight any area via xPath.

答案1

得分: 0

你在单词“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')]"));
英文:

You have a space before the word Notifications:

WebElement link = obj.driver.findElement(By.xpath(&quot;//div[contains(text(), &#39;Notifications&#39;)]&quot;));    

You should also add a wait for element before trying to find the element:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[contains(text(), &#39;Notifications&#39;)]&quot;));

WebElement link = obj.driver.findElement(By.xpath(&quot;//div[contains(text(), &#39;Notifications&#39;)]&quot;)); 

答案2

得分: 0

我在这个社区里得到了一些了不起的人的帮助,找到了问题所在。

好的,所以我的元素位于一个 iFrame 中。

为了访问这个元素,我必须首先访问这个 iFrame。

WebElement iframe = obj.driver.findElement(By.xpath("//iframe[@tabindex='-1']"));
obj.driver.switchTo().frame(iframe);
英文:

I found the issue with the help of some amazing people in this community.

Ok, so my element was in an iFrame.

xPath文本包含Selenium Web Driver

In order to access the element, I must first access the iFrame

WebElement iframe = obj.driver.findElement(By.xpath(&quot;//iframe[@tabindex=&#39;-1&#39;]&quot;));
	
obj.driver.switchTo().frame(iframe);

huangapple
  • 本文由 发表于 2020年10月28日 01:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64559799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定