无法根据后代文本节点的字符串值检索元素。

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

Unable to fetch the element based on the string value of the descendant text nodes

问题

我正试图定位基于其后代文本节点的 div 元素。

以下是 HTML 代码:

<html><head></head><body>

<div>
<div class="d x">Foo</div>
<span>Mumbai</span>
<h1>DEF</h1>
</div>

</body></html>

Selenium 代码:

driver.findElement(By.xpath("//div[.='FooMumbaiDEF']"));

根据我的理解并参考 stackoverflow 的帖子,
https://stackoverflow.com/questions/34593753/testing-text-nodes-vs-string-values-in-xpath
元素节点的字符串值是元素节点所有文本节点后代的字符串值在文档顺序中的连接。

但是我得到了 NoSuchElementException: Unable to locate element: //div[.='FooMumbaiDEF']

有任何想法为什么它无法找到该元素?

英文:

I am trying to locate the element div based on its descendant text nodes.

Below is the HTML:

&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;

&lt;div&gt;
&lt;div class=&quot;d x&quot;&gt;Foo&lt;/div&gt;
&lt;span&gt;Mumbai&lt;/span&gt;
&lt;h1&gt;DEF&lt;/h1&gt;
&lt;/div&gt;

&lt;/body&gt;&lt;/html&gt;

Selenium code:

driver.findElement(By.xpath(&quot;//div[.=&#39;FooMumbaiDEF&#39;]&quot;));

As per my understanding and after referring to the stackoverflow posts,
https://stackoverflow.com/questions/34593753/testing-text-nodes-vs-string-values-in-xpath
the string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order.

But I am getting NoSuchElementException: Unable to locate element: //div[.=&#39;FooMumbaiDEF&#39;]

Any idea why it is not able to find an element ?

答案1

得分: 1

尝试以下 XPath,因为您使用的那个是不正确的。

driver.findElement(By.xpath("//div[contains(text(),'FooMumbaiDEF')]"));
英文:

Try below xpath because the one you have used is not correct.

driver.findElement(By.xpath(&quot;//div[contains(text(),&#39;FooMumbaiDEF&#39;)]&quot;));

答案2

得分: 0

`//div[.=&#39;FooMumbaiDEF&#39;]` 不是有效的 XPath 表达式,如果您想根据元素文本处理网络元素,则可以在 XPath 表达式中使用 **contains()** 函数,该函数用于搜索包含特定文本的网络元素。

尝试以下 XPath 表达式来处理您网页中的 "Foo" 文本:

    driver.findElement(By.xpath(&quot;//div[contains(text(),&#39;Foo&#39;)]&quot;));
英文:

//div[.=&#39;FooMumbaiDEF&#39;] is not valid XPath if you want to deal with web element based on its text then you can use contains() function within Xpath expression which is used to search for the web elements that contain a particular text.

Try below xpath to handle Foo text from your webpage :

driver.findElement(By.xpath(&quot;//div[contains(text(),&#39;Foo&#39;)]&quot;));

答案3

得分: 0

根据 HTML 内容:

&lt;html&gt;
	&lt;head&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;div&gt;
			&lt;div class=&quot;d x&quot;&gt;Foo&lt;/div&gt;
			&lt;span&gt;Mumbai&lt;/span&gt;
			&lt;h1&gt;DEF&lt;/h1&gt;
		&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;

没有这样的后代文本节点。但是有后代 &lt;div&gt;&lt;span&gt;&lt;h1&gt; 节点包含了 文本

解决方案

为了找到与后代 &lt;div&gt;&lt;span&gt;&lt;h1&gt; 节点内的文本相关的父 &lt;div&gt; 元素,您可以使用以下任一基于 [tag:xpath] 的 定位策略

  • 使用后代 &lt;div&gt;

    driver.findElement(By.xpath(&quot;//div[./div[text()=&#39;Foo&#39;]]&quot;));
    
  • 使用后代 &lt;div&gt;

    driver.findElement(By.xpath(&quot;//div[./span[text()=&#39;Mumbai&#39;]]&quot;));
    
  • 使用后代 &lt;div&gt;

    driver.findElement(By.xpath(&quot;//div[./h1[text()=&#39;DEF&#39;]]&quot;));
    

理想情况下,您需要使用 WebDriverWait 来等待 visibilityOfElementLocated(),您可以使用以下任一定位器策略:

  • 使用后代 &lt;div&gt;

    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[./div[text()=&#39;Foo&#39;]]&quot;)))
    
  • 使用后代 &lt;div&gt;

    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[./span[text()=&#39;Mumbai&#39;]]&quot;)))
    
  • 使用后代 &lt;div&gt;

    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[./h1[text()=&#39;DEF&#39;]]&quot;)))
    
英文:

As per the HTML:

&lt;html&gt;
	&lt;head&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;div&gt;
			&lt;div class=&quot;d x&quot;&gt;Foo&lt;/div&gt;
			&lt;span&gt;Mumbai&lt;/span&gt;
			&lt;h1&gt;DEF&lt;/h1&gt;
		&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;

there is no descendant text nodes as such. But there are descendant &lt;div&gt;, &lt;span&gt; and &lt;h1&gt; nodes with text.


Solution

To identify the parent &lt;div&gt; element with respect to the text within the descendant &lt;div&gt;, &lt;span&gt; and &lt;h1&gt; nodes you can use either of the following [tag:xpath] based Locator Strategies:

  • Using the descendant &lt;div&gt;:

    driver.findElement(By.xpath(&quot;//div[./div[text()=&#39;Foo&#39;]]&quot;));
    
  • Using the descendant &lt;div&gt;:

    driver.findElement(By.xpath(&quot;//div[./span[text()=&#39;Mumbai&#39;]]&quot;));
    
  • Using the descendant &lt;div&gt;:

    driver.findElement(By.xpath(&quot;//div[./h1[text()=&#39;DEF&#39;]]&quot;));
    

Ideally, you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following locator strategies:

  • Using the descendant &lt;div&gt;:

    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[./div[text()=&#39;Foo&#39;]]&quot;)))
    
  • Using the descendant &lt;div&gt;:

    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[./span[text()=&#39;Mumbai&#39;]]&quot;)))
    
  • Using the descendant &lt;div&gt;:

    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[./h1[text()=&#39;DEF&#39;]]&quot;)))
    

答案4

得分: 0

请运行$x("//div/descendant::text()"),以检查div元素的所有后代文本节点。它显示了七个后代文本节点。因此,它包含四个文本节点,其数据为\n,即换行符,以及上面的3个文本节点,即FooMumbaiDEF
因此,选择上述div节点的XPath表达式应为:

$x("//div[.='\nFoo\nMumbai\nDEF\n']")
英文:

First run the $x(&quot;//div/descendant::text()&quot;) to check all the descendant text nodes of the div element. It showed seven descendant text nodes. So, it contained four text nodes having data as \n i.e. new-line character along with the above 3 text nodes i.e. Foo, Mumbai and DEF.<br/>
So, the Xpath expression to select the above div node should be:

$x(&quot;//div[.=&#39;\nFoo\nMumbai\nDEF\n&#39;]&quot;)

huangapple
  • 本文由 发表于 2020年8月24日 16:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63557265.html
匿名

发表评论

匿名网友

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

确定