如何在selenium中基于css样式类属性值定位任何元素

时间:2015-07-28 07:50:00

标签: selenium selenium-webdriver

我需要根据背景图像找到一个元素,参见截图以供参考。

<em unselectable="on" class="x-btn-split">
    ...
</em>

风格:

em.x-btn-split 
{
    background-image: url("/EXT/theme/sfdc/images/button/split_mutton_arrow.png");
}

我需要找到具有背景图片网址的元素为&#34; /EXT/theme/sfdc/images/button/split_mutton_arrow.png"

1 个答案:

答案 0 :(得分:0)

从给出的内容看,你可以在这里查看课程:

driver.find_element_by_css_selector("em.x-btn-split")

如果您坚持要检查background-image,则必须找到所有em标记并过滤掉它们,检查循环中的background-image CSS属性:

value_to_find = "/EXT/theme/sfdc/images/button/split_mutton_arrow.png"

try:
    em = next(em for em in driver.find_elements_by_css_selector("em.x-btn-split") 
              if em.value_of_css_property("background-image") == value_to_find)
    print("Found!")
    print(em)
except StopIteration:
    print("Not Found!")

(Python中的例子)