如何使用Selenium获取ID

时间:2017-10-30 16:35:09

标签: python selenium-webdriver

我想在类中获取ID的值。例如:

<div class="a_class_name_a" an_identifier="1" id="the_requered_id_like_54656"></div>
<div class="a_class_name_a" an_identifier="2" id="the_requered_id_like_4565"></div>
<div class="a_class_name_a" an_identifier="3" id="the_requered_id_like_12"></div>
<div class="a_class_name_b" an_identifier="4" id="the_requered_id_like_786"></div>
<div class="a_class_name_c" an_identifier="5" id="the_requered_id_like_566"></div>

我如何获得值&#34; the_requered_id_like_786&#34; id与an_identifier =&#34; 4&#34;作为我使用Selenium Webdriver和Python的选择标准?

1 个答案:

答案 0 :(得分:0)

你可以这样做:

id= driver.find_element_by_xpath("//div[@class='a_class_name_b' and @an_identifier='4']").get_attribute("id")
print(id)