英文:
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:
<html><head></head><body>
<div>
<div class="d x">Foo</div>
<span>Mumbai</span>
<h1>DEF</h1>
</div>
</body></html>
Selenium code:
driver.findElement(By.xpath("//div[.='FooMumbaiDEF']"));
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[.='FooMumbaiDEF']
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("//div[contains(text(),'FooMumbaiDEF')]"));
答案2
得分: 0
`//div[.='FooMumbaiDEF']` 不是有效的 XPath 表达式,如果您想根据元素文本处理网络元素,则可以在 XPath 表达式中使用 **contains()** 函数,该函数用于搜索包含特定文本的网络元素。
尝试以下 XPath 表达式来处理您网页中的 "Foo" 文本:
driver.findElement(By.xpath("//div[contains(text(),'Foo')]"));
英文:
//div[.='FooMumbaiDEF']
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("//div[contains(text(),'Foo')]"));
答案3
得分: 0
根据 HTML 内容:
<html>
<head>
</head>
<body>
<div>
<div class="d x">Foo</div>
<span>Mumbai</span>
<h1>DEF</h1>
</div>
</body>
</html>
没有这样的后代文本节点。但是有后代 <div>
、<span>
和 <h1>
节点包含了 文本。
解决方案
为了找到与后代 <div>
、<span>
和 <h1>
节点内的文本相关的父 <div>
元素,您可以使用以下任一基于 [tag:xpath] 的 定位策略:
-
使用后代
<div>
:driver.findElement(By.xpath("//div[./div[text()='Foo']]"));
-
使用后代
<div>
:driver.findElement(By.xpath("//div[./span[text()='Mumbai']]"));
-
使用后代
<div>
:driver.findElement(By.xpath("//div[./h1[text()='DEF']]"));
理想情况下,您需要使用 WebDriverWait 来等待 visibilityOfElementLocated()
,您可以使用以下任一定位器策略:
-
使用后代
<div>
:new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[./div[text()='Foo']]")))
-
使用后代
<div>
:new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[./span[text()='Mumbai']]")))
-
使用后代
<div>
:new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[./h1[text()='DEF']]")))
英文:
As per the HTML:
<html>
<head>
</head>
<body>
<div>
<div class="d x">Foo</div>
<span>Mumbai</span>
<h1>DEF</h1>
</div>
</body>
</html>
there is no descendant text nodes as such. But there are descendant <div>
, <span>
and <h1>
nodes with text.
Solution
To identify the parent <div>
element with respect to the text within the descendant <div>
, <span>
and <h1>
nodes you can use either of the following [tag:xpath] based Locator Strategies:
-
Using the descendant
<div>
:driver.findElement(By.xpath("//div[./div[text()='Foo']]"));
-
Using the descendant
<div>
:driver.findElement(By.xpath("//div[./span[text()='Mumbai']]"));
-
Using the descendant
<div>
:driver.findElement(By.xpath("//div[./h1[text()='DEF']]"));
Ideally, you need to induce WebDriverWait for the visibilityOfElementLocated()
and you can use either of the following locator strategies:
-
Using the descendant
<div>
:new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[./div[text()='Foo']]")))
-
Using the descendant
<div>
:new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[./span[text()='Mumbai']]")))
-
Using the descendant
<div>
:new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[./h1[text()='DEF']]")))
答案4
得分: 0
请运行$x("//div/descendant::text()")
,以检查div
元素的所有后代文本节点。它显示了七个后代文本节点。因此,它包含四个文本节点,其数据为\n
,即换行符,以及上面的3个文本节点,即Foo
,Mumbai
和DEF
。
因此,选择上述div
节点的XPath表达式应为:
$x("//div[.='\nFoo\nMumbai\nDEF\n']")
英文:
First run the $x("//div/descendant::text()")
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("//div[.='\nFoo\nMumbai\nDEF\n']")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论