0

I'm trying to click the "Login" button on this website, with Selenium : https://results.decisiondeskhq.com/2020/primary/colorado/president. I right clicked the element in inspect element, copied the xpath, and put it into the find_element_by_xpath function.

Here's my code:

from selenium import webdriver

driver = webdriver.Chrome(executable_path="/users/aliallam/Desktop/scraper test/chromedriver")
url = 'https://results.decisiondeskhq.com/2020/primary/colorado/president'
driver.get(url)

driver.find_element_by_xpath('//*[@id="content"]/div/div/div/div/button').click()

This is the error message I get:

Traceback (most recent call last):
  File "/Users/aliallam/Desktop/scraper test/sandbox2.py", line 7, in <module>
    driver.find_element_by_xpath('//*[@id="content"]/div/div/div/div/button').click()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="content"]/div/div/div/div/button"}
  (Session info: chrome=80.0.3987.149)

Thank you in advance!

Ali Allam
  • 59
  • 1
  • 5
  • This website appears to have a loading screen. Try using a wait command to make sure the page has loaded before trying to click. Check out this website for an in depth explanation of how to use wait commands in selenium https://www.browserstack.com/guide/wait-commands-in-selenium-webdriver – Adam Johnston Mar 25 '20 at 04:40

2 Answers2

0

The element has an unique id, so instead of using xpath, you should use the id and you should apply explicit wait on the element so that the script waits until the element is present and as there is a div of element present above the element that you are trying to click, you need to use java script click in this case.
Your code should be like:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path="/users/aliallam/Desktop/scraper test/chromedriver")
url = 'https://results.decisiondeskhq.com/2020/primary/colorado/president'
driver.get(url)

element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "login-text-btn")))
driver.execute_script("arguments[0].click();", element)
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
0

You need to wait until the webpage load to select the button. You need to import

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

adding the delay and wait before select the element like this:

delay = 10  # seconds
WebDriverWait(driver, delay).until(
    EC.presence_of_element_located((By.CLASS_NAME, 'signup-boxes')))
# you can select element that you want ini here

for more resource visit here

dhentris
  • 198
  • 1
  • 2
  • 10