Selenium通过部分链接文本找不到链接

时间:2015-11-11 16:40:28

标签: python selenium

我有以下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="{&quot;code&quot;:&quot;&quot;,&quot;name&quot;:&quot;Series&quot;,&quot;sort&quot;:&quot;4&quot;}">Series</li><li style="border-bottom: 1px solid rgb(170, 170, 170);" title="Consumer Price Indices (preceding month=100), Food" code="A01010601" node="{&quot;code&quot;:&quot;A01010601&quot;,&quot;name&quot;:&quot;Consumer Price Indices (preceding month=100), Food&quot;,&quot;sort&quot;:&quot;1&quot;}">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="{&quot;code&quot;:&quot;A01010602&quot;,&quot;name&quot;:&quot;Consumer Price Indices (preceding month=100), Grain&quot;,&quot;sort&quot;:&quot;1&quot;}">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="{&quot;code&quot;:&quot;A01010603&quot;,&quot;name&quot;:&quot;Consumer Price Indices (preceding month=100), Meat, _Poultry and Processed Products&quot;,&quot;sort&quot;:&quot;1&quot;}">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="{&quot;code&quot;:&quot;A01010604&quot;,&quot;name&quot;:&quot;Consumer Price Indices (preceding month=100), Eggs&quot;,&quot;sort&quot;:&quot;1&quot;}">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="{&quot;code&quot;:&quot;A01010605&quot;,&quot;name&quot;:&quot;Consumer Price Indices (preceding month=100), Aquatic _Products&quot;,&quot;sort&quot;:&quot;1&quot;}">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="{&quot;code&quot;:&quot;A01010606&quot;,&quot;name&quot;:&quot;Consumer Price Indices (preceding month=100), Fresh _Vegetables&quot;,&quot;sort&quot;:&quot;1&quot;}">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="{&quot;code&quot;:&quot;A01010607&quot;,&quot;name&quot;:&quot;Consumer Price Indices (preceding month=100), Fresh _Fruits&quot;,&quot;sort&quot;:&quot;1&quot;}">Consumer Price Indices (preceding month=100), Fresh _Fruits</li></ul>

显然,其中有"Consumer Price Indices (preceding month=100), Food"的链接,但我收到"unable to locate element"错误。我做错了什么?

1 个答案:

答案 0 :(得分:2)

要扩展@ That1Guy所指出的内容,您所提供的菜单中没有链接a元素,但是部分链接文字&#34; locator只会检查a元素的文本。

相反,请按文字找到li元素:

menu.find_element_by_xpath(".//li[. = '%s']" % vertical_element).click()