繁体   English   中英

Selenium xpath all(// *)不会占用每个css元素

[英]Selenium xpath all (//*) doesn't take every css elements

我正在尝试使用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来定位包含参数“background”或“color”的每个css属性(来自css文件)?

Selenium不处理DOM结构中缺少的CSS属性。 或者,您可以使用jQuery.filter()为每个需要的节点应用一个允许以标准方式查找元素的类:对于我的示例,下面是//div[contains(@class, 'found')]

 $(document).ready(function() { $("*").filter(function() { return $(this).css("background-color") == "rgb(54, 64, 66)"; }).text("true").addClass("found"); }); 
 .white { background-color: rgb(255, 255, 255); } .black { background-color: rgba(0, 0, 0, 0); } .carmine { background-color: rgba(169, 68, 66, 1); } .cosmos { background-color: rgb(54, 64, 66); } .found { color: green !important; font-weight: bold; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="white">false</div> <div class="black">false</div> <div class="carmine">false</div> <div class="cosmos">false</div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM