简体   繁体   中英

Python: Selenium no such element: Unable to locate element: {“method”:“xpath”,“selector”:“//button[@data-id=1]”}

Why I got this error no such element: Unable to locate element: {"method":"xpath","selector":"//button[@data-id=1]"}

test.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

I have also tried with self.browser.find_element_by_id('update_id_'+str(zone.id)) but not working:( what's wrong going on? 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>

From the error message and the code for Button, I could see that zone.id value is coming wrong. The value of zone.id is coming as 1 instead of 2.

You can try using,

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

or

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

If you can give bit more HTML code, I can give you the implementation of above using find_element_by_xpath .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM