I type the correct xpath from a button, but Selenium says xpath does not exist

Asked

Viewed 2,088 times

0

I’m making a bot in python using Selenium and need to click the Instagram like button using code.

To find the Like button I’m using the method find_element_by_xpath().

To make sure, I clicked on inspect element in Chrome and clicked on "Copy Xpath", since the xpath of that button never changes. But when I run the code, it gives the following error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

The code is like this:


like_button = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/article/div[2]/section[1]/span[1]/button')
like_button.click()

But for some reason it doesn’t work

  • Have you ever given a Sleep() before the find_element_by_xpath? Just to make sure the page has loaded.

  • Yes, I’ve come to Aar a 20-second Sleep, but he still says it doesn’t exist

2 answers

0

I don’t know how this structure of html, but I believe that if you change the location of click() do up to the span instead of the button.

like_button = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/article/div[2]/section[1]/span[1]')

like_button.click()

should fix this problem

0


This message is saying that the element is not in the GIFT when you searched for it, it would be prudent to expect it to be ~available~ on the screen for some time.

First, I flipped on instagram and it seems that your xpath ta wrong, change your xpath to:

//*[@id="react-root"]/section/main/section/div/div/article/div[2]/section[1]/span[1]/button

Second, do not advise you to use the path this way, any change will cause problems in your script, how about elaborate it a little more? For example go to the 'like' element based on some text you are looking for? Here is an xpath that will return the likes based on the profile name, in the case SPENCER_MELO:

//a[text()='SPENCER_MELO']/ancestor::article//span[@aria-label='Like']

About waiting before clicking, follow how to do below.

Necessary imports in this example:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

Use:

# Da pra trocar por xpath se quiser.
like_css = '#react-root span[aria-label=\'Like\']'

element = WebDriverWait(self.driver, 30).until(expected_conditions.element_to_be_clickable((By.CSS, like_css)))
element.click()
  • Spencer Melo, I tried and gave an error: File "C: Users luisg Appdata Local Programs Python Python37-32 lib site-Packages Selenium webdriver support Wait.py", line 80, in until raise Timeoutmessage(exception message, screen, stacktrace) Selenium.common.exceptions.Timeoutexception: Message: found it strange pq dps from Message: <br>did not appear at all. Can you help me?

  • This element you are looking for is not available in the DOM or it is inside an iframe, look in the html if you have some "iframe", something else, edit your question with the html of the button, it is easier for us to answer.

  • sorry, I didn’t understand what you meant by the button html. Just the button element? Anyway, is the like button of the urge, they all have the same values with the same attributes, just take a look there pq I really didn’t understand what the html button means :/

  • Well, Voce generated this xpath here //*[@id="react-root"]/section/main/div/div/article/div[2]/section[1]/span[1]/button looking (inspect) the html of the boot in the browser, it would be good if Voce put the html code in the question, so we can see how the html structure.

  • @Luismeirelles gave an important edited answer, see if she can finally answer your question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.