Ben*_*hak 5 python selenium xpath web2py
我有一个包含许多下载链接的表格的网页,我想让 selenium 点击最后一个:
桌子:
item1 Download
item2 Download
item3 Download
Run Code Online (Sandbox Code Playgroud)
selenium 一定要点击Downloadnextitem3
我使用 xpath 查找所有元素,然后以这种方式获取返回数组或字典的大小
x = bot._driver.find_element_by_xpath("//a[contains(text(),'Download')]").size()
Run Code Online (Sandbox Code Playgroud)
但我总是收到这个错误
TypeError: 'dict' object is not callable
Run Code Online (Sandbox Code Playgroud)
我尝试使用get_xpath_countmethode,但python中的selenium中不存在methode!
我想到了另一种解决方案,但我不知道该怎么做,如下所示
x = bot._driver.find_element_by_xpath("(//a[contains(text(),'Download')])[size()-1]")
Run Code Online (Sandbox Code Playgroud)
或者
x = bot._driver.find_element_by_xpath("(//a[contains(text(),'Download')])[last()]")
Run Code Online (Sandbox Code Playgroud)
或者是其他东西
最好的方法是使用 JavaScript 并按类名查找元素
self.bot._driver.execute_script("var e = document.getElementsByClassName('download'); e[e.length-1].click()")
Run Code Online (Sandbox Code Playgroud)
来源: http ://de.slideshare.net/adamchristian/javascript-testing-via-selenium
| 归档时间: |
|
| 查看次数: |
12811 次 |
| 最近记录: |