簡體   English   中英

Python: Selenium 沒有這樣的元素: 無法定位元素: {“method”:“xpath”,“selector”:“//button[@data-id=1]”}

[英]Python: Selenium no such element: Unable to locate element: {“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為您提供上述實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM