[英]Can't get text from element in selenium from outside tag?
I have the following element 我有以下要素
<td nowrap="nowrap" align="left" colspan="2">
<b>Zone: </b>
"521X 7SW "
</td>
How do I grab the text containing "521X 7SW"? 如何获取包含“ 521X 7SW”的文本? Everytime I try the .text command in selenium it only returns "Zone:".
每次我在selenium中尝试.text命令时,它只会返回“ Zone:”。 If I use the get_attibute("text") command it returns nothing.
如果我使用get_attibute(“ text”)命令,则不会返回任何内容。
I've already selected the element with this command 我已经使用此命令选择了元素
zone = driver.find_element_by_xpath("//*[contains(text(), 'Zone')]")
And the text isn't invisible. 文字不是不可见的。 Here's a picture of the element
这是元素的图片
That is a text node not just text. 那是一个文本节点,而不仅仅是文本。
You can inject Javascript like this : 您可以像这样注入Javascript:
text_node = driver.find_element_by_xpath("//b[contains(text(),'Zone: ')]")
data = driver.execute_script("return arguments[0].nextSibling.textContent;", text_node)
print(data)
text_node is passed into Javascript as arguments[0]
text_node作为
arguments[0]
传递给Javascript arguments[0]
Text which you are looking for is not related to ' b ' tag , it's text()
value of td tag . 您要查找的文本与' b '标签无关,它是td标签的
text()
值。 You should find td tag text to get "521X 7SW" value. 您应该找到td标签文本以获取“ 521X 7SW”值。
driver.find_element_by_xpath("//td[@nowraap='nowrap']/text()");
You can optimize xpath of td, as I have used only displayed HTML in your question . 您可以优化td的xpath,因为我在问题中仅使用了显示的HTML 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.