Python: Selenium no such元素: 无法定位元素: { "method" : "xpath" , "selector" : "//button[@data-id=1]" }



为什么我得到这个错误no such element: Unable to locate element: {"method":"xpath","selector":"//button[@data-id=1]"}

测试.py

zone = Zone.objects.last()
self.browser.refresh()
time.sleep(2)
self.browser.find_element_by_xpath("//button[@data-id="+str(zone.id)+"]").click() # zone.id = 1

我也尝试过使用self.browser.find_element_by_id('update_id_'+str(zone.id((,但不起作用:(怎么了?html

<button type="button" id="updateButton update_id_2" class="btn btn-xs btn-primary updateButton" data-id="2">
<i class="fa fa-edit"></i>
</button>

从错误消息和Button的代码中,我可以看到zone.id值出现了错误。zone.id的值是1,而不是2。

你可以尝试使用,

self.browser.find_element_by_css_selector('.btn.btn-xs.btn-primary.updateButton').click()

self.browser.find_element_by_id('updateButton update_id_2').click()

如果你能给出更多的HTML代码,我可以用find_element_by_xpath给你上面的实现。

最新更新