【发布时间】:2013-08-23 00:23:46
【问题描述】:
我有一个包含许多下载链接的表格的网页 我想让 selenium 点击最后一个:
表:
item1 Download
item2 Download
item3 Download
selenium 必须点击Download 下一个item3
我使用xpath查找所有元素然后以这种方式获取返回数组或dict的大小
x = bot._driver.find_element_by_xpath("//a[contains(text(),'Download')]").size()
但我总是得到这个错误
TypeError: 'dict' object is not callable
我尝试使用get_xpath_count 方法,但该方法在 python 中的 selenium 中不存在!
我想过另一种解决方案,但我不知道该怎么做,如下所示
x = bot._driver.find_element_by_xpath("(//a[contains(text(),'Download')])[size()-1]")
或
x = bot._driver.find_element_by_xpath("(//a[contains(text(),'Download')])[last()]")
或者其他的
【问题讨论】:
-
可以打印
bot._driver.find_element_by_xpath("//a[contains(text(),'Download')]")的结果吗? -
size 是一个属性。如果您想查找与您的
xpath匹配的所有项目,请使用find_elements_by_xpath(注意s)。 -
@kroolik,是的,它现在与“s”一起工作,但它返回一个空列表,
size()不起作用,但len(x)起作用。另一方面bot._driver.find_element_by_xpath("(//a[contains(text(),'Download')])[5]").click()without "s" 有效!那么为什么find_elements_by_xpath返回空列表:/
标签: python xpath selenium web2py