Selenium 和 Python 查找元素和文本? [英] Selenium and Python to find elements and text?

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

问题描述

当我访问某个网页时,我试图找到某个元素和一段文本:

When I go to a certain webpage I am trying to find a certain element and piece of text:

<span class="Bold Orange Large">0</span>

这不起作用:(它给出了复合类名的错误...)

elem = browser.find_elements_by_class_name("Bold Orange Large")

所以我尝试了这个:(但我不确定它是否有效,因为我并不真正理解在 selenium 中执行 css 选择器的正确方法......)

elem = browser.find_elements_by_css_selector("span[class='Bold Orange Large']")

找到span元素后,我想找到(内容)里面的数字.

Once I find the span element, I want to find the number that is inside (the content).

num = elem.(what to put here??)

任何有关 CSS 选择器、类名和查找元素文本的帮助都会很棒!

Any help with CSS selectors, class names, and finding element text would be great!

谢谢.

哦!我的另一个问题是这些精确的跨度元素有多个,但里面有不同的数字.我该如何处理?

Oh! and my other problem is that there are multiple of those exact span elements but with different numbers inside. How can I deal with that?

推荐答案

你想要:elem.text 得到里面的数字".

you want: elem.text to get "the number that is inside".

说明:

在您的示例中,elem 是 webdriver 的 WebElement 类的实例(来自 selenium.webdriver.remote.webelement)

in your example, elem is an instance of webdriver's WebElement class (from selenium.webdriver.remote.webelement)

一个 WebElement 实例有几个属性,包括:

a WebElement instance has several properties, including:

  • tag_name
  • text
  • 尺寸
  • 位置
  • id

.text 属性包含内容"

"而我的另一个问题是这些精确跨度元素的倍数..我该如何处理?"

"and my other problem is that there are multiple of those exact span elements ..how can I deal with that?"

请不要在一个问题中提出几个问题......它不适用于 SO 的格式.

please don't ask several questions in one.. it doesn't work well with SO's format.

但基本上,使用 find_elements_* 而不是 find_element_*.然后,您的定位器可以返回匹配 WebElement 实例的列表,而不是单个匹配实例...您可以进一步过滤.

but basically, use the find_elements_* instead of find_element_*. your locator can then return a list of matching WebElement instances instead of a single matching instance... which you can filter further.

这篇关于Selenium 和 Python 查找元素和文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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