Selenium - 无法从 span 元素获取文本 [英] Selenium - cant get text from span element

查看:84
本文介绍了Selenium - 无法从 span 元素获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 Selenium 获取文本感到非常困惑.

span 标签,其中包含一些文本.当我使用 driver.find_element_by_... 搜索它们时,一切正常.

但问题是无法从中获取文本.

span 标签被找到,因为我不能使用 .get_attribute('outerHTML') 命令,我可以看到这个:

ThrivingHealthy

但是如果我将 .get_attribute('outerHTML') 更改为 .text 它将返回空文本,正如您在上面看到的那样不正确.

这是示例(输出是字典):

display_site = element.find_element_by_css_selector('span.branding').get_attribute('outerHTML')

<块引用>

'display_site': u'<span class="branding">ThrivingHealthy</span>'

display_site = element.find_element_by_css_selector('span.branding').text

<块引用>

'display_site': u''

你可以清楚地看到,有一个文本,但它没有找到它.可能有什么问题?

我找到了一种解决方法.我刚刚将 .text 更改为 .get_attribute('innerText')

但我仍然很好奇为什么它会这样工作?

解决方案

问题是有很多标签是使用 span.branding 获取的.当我使用 find_elements(复数)查询该页面时,它返回了 20 个标签.每个标签似乎都加倍了......我不知道为什么,但我的猜测是一组隐藏而另一组可见.据我所知,这对中的第一个是隐藏的.这可能就是您无法从中提取文本的原因.Selenium 的设计是不与用户无法交互的元素进行交互.这可能就是为什么您可以获取元素但是当您尝试提取文本时它不起作用的原因.最好的办法是使用 find_elements 拉出整个集合,然后循环遍历获取文本的集合.您将像 20 一样循环,只从 10 中获取文本,但看起来您仍然会得到整个集合.这很奇怪,但它应该可以工作.

I'm very confused by getting text using Selenium.

There are span tags with some text inside them. When I search for them using driver.find_element_by_..., everything works fine.

But the problem is that the text can't be got from it.

The span tag is found because I can't use .get_attribute('outerHTML') command and I can see this:

<span class="branding">ThrivingHealthy</span>

But if I change .get_attribute('outerHTML') to .text it returns empty text which is not correct as you can see above.

Here is the example (outputs are pieces of dictionary):

display_site = element.find_element_by_css_selector('span.branding').get_attribute('outerHTML')

'display_site': u'<span class="branding">ThrivingHealthy</span>'

display_site = element.find_element_by_css_selector('span.branding').text

'display_site': u''

As you can clearly see, there is a text but it does not finds it. What could be wrong?

EDIT: I've found kind of workaround. I've just changed the .text to .get_attribute('innerText')

But I'm still curious why it works this way?

解决方案

The problem is that there are a LOT of tags that are fetched using span.branding. When I just queried that page using find_elements (plural), it returned 20 tags. Each tag seems to be doubled... I'm not sure why but my guess is that one set is hidden while the other is visible. From what I can tell, the first of the pair is hidden. That's probably why you aren't able to pull text from it. Selenium's design is to not interact with elements that a user can't interact with. That's likely why you can get the element but when you try to pull text, it doesn't work. Your best bet is to pull the entire set with find_elements and then just loop through the set getting the text. You will loop through like 20 and only get text from 10 but it looks like you'll still get the entire set anyway. It's weird but it should work.

这篇关于Selenium - 无法从 span 元素获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆