【问题标题】:Get color of span class inside h1 tag获取 h1 标签内 span 类的颜色
【发布时间】:2021-04-16 10:10:42
【问题描述】:

我想使用 Selenium 获取网页中所有标题元素实例的颜色。

h1 元素为例,如果我尝试:

h1 = driver.find_element_by_tag_name('h1')
color = elem_tag.value_of_css_property('color')

它只给了我一种颜色的值,甚至不是我检查元素时看到的颜色。我明白这是因为它被覆盖了。所以我认为我需要做的是获取实例的颜色 [?]。

我看到h1标签里面有这个span类,但是我不知道如何获取它的颜色:

<h1 class="ccl-2a4b5924 ccl-29aecca026 ccl-a1841d8ca6 ccl-0338edd3d4 ccl-61059584b">
    <span class="example_class-5c86233">SOME TEXT</span>
</h1>

如何获取网页上显示的“SOME TEXT”的颜色?

【问题讨论】:

  • 虽然我不知道 Selenium,但在 Javascript 中,您必须遍历这些元素才能返回每个元素的值。我认为这里的情况是一样的。
  • 可以提供网址吗?
  • @SwaroopHumane 在这里:deliveroo.be/en
  • @LucasFarias 在首页只有 1 个 h1 标签可用。仅供参考
  • @SwaroopHumane 不错,但是为什么我会得到“错误”的颜色值,即后来被覆盖的颜色值?

标签: python html css selenium


【解决方案1】:

这就是你要找的 -

from selenium.webdriver.support.color import Color

rgb = driver.find_element_by_xpath("//h1/span").value_of_css_property('color')
hex = Color.from_string(rgb).hex
print("HEX:- " + hex)

如果这是您要查找的内容,请将其标记为答案。

【讨论】:

  • 它返回与我正在使用的代码相同的颜色值(即#ffffff),但我想要#440063。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
  • 2022-08-11
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多