Trevar
Trevar

Reputation: 1

Xpath exists in chrome, but not in my script. no such element: Unable to locate element:

I am trying to find the button element by xpath, which is found when I type it in chrome, but the script gives me the no attribute id error. I have tried switching into an iframe and frame, I have used webdriver.wait to wait for the button element to show and none of those work. I would also like to cycle through and click the first button if it says "Follow" and then move to the next button if it says "Follow".The Script runs on chrome and I am trying to do this on instagram Html here

 popup = browser.find_element_by_xpath('//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]')

 ActionChains(browser)\
.move_to_element(popup).click()\
.perform()

File "/Users/trevaroneill/PycharmProjects/Insta/instafollow.py", line 91, in <module> popup = browser.find_element_by_xpath('//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]')

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]"}

Upvotes: 0

Views: 590

Answers (2)

Nathan Ripert
Nathan Ripert

Reputation: 890

Since you are using find_elements, you should rather write:

 ActionChains(browser)\
.move_to_element(popup[0]).click()\
.perform()

in order to access first element of the list returned by find_elements.

The problem is if you have several webelements selected by your xpath, in this case, it is not sure that the first one is the one you actually aim at. I would suggest you use find_element if there are no particular reason for which you are using find_elements

Upvotes: 1

Yoav Gaudin
Yoav Gaudin

Reputation: 77

You are getting the wanted element in a list.

change popup = browser.find_elements_by_xpath('//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]')

into popup = browser.find_element_by_xpath('//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]')

find_element and not find_elements.

Upvotes: 0

Related Questions