【发布时间】:2019-03-12 07:12:38
【问题描述】:
我正在尝试使用 selenium (XPath) 列出不同网站上的每种颜色,但我不知道为什么我的脚本没有得到所有颜色。
background_ele = browser.find_elements_by_xpath("//*[contains(@style,'background')]")
colors_ele = browser.find_elements_by_xpath("//*[contains(@style,'color')]")
background_colors = [x.value_of_css_property('background-color') for x in background_ele]
colors = [x.value_of_css_property('background-color') for x in colors_ele]
此代码应该获取具有背景或颜色属性的每个元素,但是当我为此网站运行它时:“www.example.com”我看不到下面出现在页脚和页眉上的颜色:
background-color: rgb(54, 64, 66) !important;
我只打印那些:
['rgba(255, 255, 255, 0)', 'rgba(0, 0, 0, 0)', 'rgba(169, 68, 66, 1)', 'rgba(0, 0, 0, 0)']
我的代码是否存在问题,或者可能是使用 selenium 的更有效方法?
更新
我的脚本实际上只接受 html 中的标签,而不是 css 文件中的标签。
<div class="example"style="src="https://example.com/img/slider.jpg"></div>
如何使用 selenium 定位每个包含参数“背景”或“颜色”的 css 属性(来自 css 文件)?
【问题讨论】:
-
看起来有些
styles是在单独的样式文本/css 块中定义的。尝试获取这些并寻找backgroundColors。 -
@wp78de 我怎样才能找到那些有硒的?它不应该自动捕获它们吗?
-
@jjyoh 你想得到所有有效计算的颜色吗?