我有以下Python代码片段(完整的东西太长了):
menu=driver.find_element_by_class_name("dtList")
for vertical_element in verticallist:
menu.find_element_by_partial_link_text(vertical_element).click()
这是print vertical_element
的样子(数据类型为unicode
):
"Consumer Price Indices (preceding month=100), Food"
以下是print menu.get_attribute('innerHTML')
给我的信息:
<ul><li style="border-bottom: 1px solid rgb(170, 170, 170);" node="{"code":"","name":"Series","sort":"4"}">Series</li><li style="border-bottom: 1px solid rgb(170, 170, 170);" title="Consumer Price Indices (preceding month=100), Food" code="A01010601" node="{"code":"A01010601","name":"Consumer Price Indices (preceding month=100), Food","sort":"1"}">Consumer Price Indices (preceding month=100), Food</li><li style="border-bottom: 1px solid rgb(170, 170, 170);" title="Consumer Price Indices (preceding month=100), Grain" code="A01010602" node="{"code":"A01010602","name":"Consumer Price Indices (preceding month=100), Grain","sort":"1"}">Consumer Price Indices (preceding month=100), Grain</li><li style="border-bottom: 1px solid rgb(170, 170, 170);" title="Consumer Price Indices (preceding month=100), Meat, _Poultry and Processed Products" code="A01010603" node="{"code":"A01010603","name":"Consumer Price Indices (preceding month=100), Meat, _Poultry and Processed Products","sort":"1"}">Consumer Price Indices (preceding month=100), Meat, _Poultry and Processed Products</li><li style="border-bottom: 1px solid rgb(170, 170, 170);" title="Consumer Price Indices (preceding month=100), Eggs" code="A01010604" node="{"code":"A01010604","name":"Consumer Price Indices (preceding month=100), Eggs","sort":"1"}">Consumer Price Indices (preceding month=100), Eggs</li><li style="border-bottom: 1px solid rgb(170, 170, 170);" title="Consumer Price Indices (preceding month=100), Aquatic _Products" code="A01010605" node="{"code":"A01010605","name":"Consumer Price Indices (preceding month=100), Aquatic _Products","sort":"1"}">Consumer Price Indices (preceding month=100), Aquatic _Products</li><li style="border-bottom: 1px solid rgb(170, 170, 170);" title="Consumer Price Indices (preceding month=100), Fresh _Vegetables" code="A01010606" node="{"code":"A01010606","name":"Consumer Price Indices (preceding month=100), Fresh _Vegetables","sort":"1"}">Consumer Price Indices (preceding month=100), Fresh _Vegetables</li><li style="border-bottom: medium none;" title="Consumer Price Indices (preceding month=100), Fresh _Fruits" code="A01010607" node="{"code":"A01010607","name":"Consumer Price Indices (preceding month=100), Fresh _Fruits","sort":"1"}">Consumer Price Indices (preceding month=100), Fresh _Fruits</li></ul>
显然,其中有"Consumer Price Indices (preceding month=100), Food"
的链接,但我收到"unable to locate element"
错误。我做错了什么?
答案 0 :(得分:2)
要扩展@ That1Guy所指出的内容,您所提供的菜单中没有链接a
元素,但是部分链接文字&#34; locator只会检查a
元素的文本。
相反,请按文字找到li
元素:
menu.find_element_by_xpath(".//li[. = '%s']" % vertical_element).click()