简体   繁体   English

XPATH找不到Web上包含的文本-使用contains(text())

[英]XPATH not finding the text that is contained on the web - using contains(text())

On this website https://classicdb.ch/?quest=788 在此网站上https://classicdb.ch/?quest=788

I tried: 我试过了:

driver.find_element_by_xpath(
                "//div[contains(text(), 'Start')]").text

It finds the element and it returns 找到元素并返回

'Start: Kaltunk'

However when I try to find the element that contains "End" it doesn't finds anything. 但是,当我尝试查找包含“ End”的元素时,它什么也找不到。

driver.find_element_by_xpath(
                "//div[contains(text(), 'End')]").text

Why is this? 为什么是这样?

Thank you. 谢谢。

Try with the below xpath. 尝试使用下面的xpath。

//table//div[contains(.,'End:')]

Screenshot: 屏幕截图:

在此处输入图片说明

Explanation: Edit 1 说明:编辑1

First of all let's see how many text() nodes are present under the target div . 首先,让我们看一下目标div下有多少个text()节点。

在此处输入图片说明

So the div have 3 text nodes. 因此div有3个文本节点。

Let me elaborate the original xpath used by OP. 让我详细说明OP使用的原始xpath。

//div[contains(text(), 'End')]
 ^div present anywhere in the document
     ^which contains
               ^the **first** text() node with string value as `End`

When contains() is given as its first argument (in div[argument] ), it takes the string value of the first node, but End appears in the second text node, not the first. 如果将contains()作为其第一个参数(在div [argument]中 ),它将采用第一个节点的字符串值,但是End出现在第二个文本节点中,而不是第一个。 That's the reason why the xpath did not worked. 这就是xpath无法正常工作的原因。

We have 2 options to handle this. 我们有2个选项来处理此问题。

1) using text() as the first argument - By that way it will get all text nodes under current context and then use contains() as a condition to check for the text() value that will match any text() node whose value contains End as shown below. 1)使用文本()作为第一个参数 -通过这种方式,将获得当前环境下的所有文本节点,然后用contains()作为条件来检查text()值,将匹配其值的任何文本()节点包含End ,如下所示。

//div[text()[contains(., 'End')]]
^div present any where in the document
     ^which have text() node
             ^ that contains 'End`

Check the below screenshot: 检查以下屏幕截图:

在此处输入图片说明

By this time, you would got a question then why the first xpath ( //div[contains(text(), 'Start')] ) used by OP worked? 到这个时候,您会问一个问题,为什么OP使用的第一个xpath( //div[contains(text(), 'Start')] )起作用?

If you look at the text() nodes associated in the div, Start text is present in the 1st text() node itself, that's the reason why he was able to use that xpath. 如果查看div中关联的text()节点,则Start文本将出现在第一个text()节点本身中,这就是他能够使用该xpath的原因。

在此处输入图片说明

2) Using . 2)使用。 to check in current node context In simple terms when you say . 当您说时,可以简单地检查当前节点的上下文 . it will check in the entire current element context for the End . 它将在整个当前元素上下文中检查End

//div[contains(.,'End')]

If you don't limit the scopt to //table (at the beginning of the xpath) you will get 5 divs as the ancestor divs of the original div which have this text also be matched with the xpath. 如果不将scopt限制为//table (在xpath的开头),则将获得5个div作为原始div的祖先div,该文本的文本也与xpath匹配。 So limit the scope to check with in the table like `//table//div[contains(.,'End')] 因此,限制在表中检查的范围,例如`//table//div[contains(.,'End')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM