Unable to locate button in selenium

I am trying to get an element from a website and I would get this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div[3]/div[1]/div[1]/button"}

I got the element by copying the XPath in inspect element and made sure the element exists and is clickable. Here is my code:

driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[1]/div[1]/button").click()

Here is the HTML code:

<button class="styled joinRound" data-text="joinGame">Join game</button>

How can I fix this?

Answer

It may be easier to test if you include the URL you’re working with.

You can try finding it by text instead:

driver.find_elements_by_xpath("//*[contains(text(), 'Join game')]").click()

Click button by text using Python and Selenium

This may also help. It’ll record your clicks and may give you the right path you need.

https://chrome.google.com/webstore/detail/katalon-recorder-selenium/ljdobmomdgdljniojadhoplhkpialdid?hl=en-US