Search code examples
pythonseleniumselenium-webdriverselenium-chromedrivernosuchelementexception

Unable to locate element - selenium webdriver python


So I tried to click a button using selenium webdriver in Python. It didn't find the element (in this case a class name), even with enough time to load the element.

I got this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".account-item login-btn waves-effect"}

here is my code:

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

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://freecash.com")
time.sleep(3)

signIn = driver.find_element(By.CLASS_NAME, "account-item login-btn waves-effect")
signIn.click()

Please help me with this it would be greatly appreciated. If you need more info just ask down below. :)


Solution

  • To click on the element with text Sign In you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      driver.execute("get", {'url': 'https://freecash.com'})
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.account-item.login-btn span"))).click()
      
    • Using XPATH:

      driver.execute("get", {'url': 'https://freecash.com'})
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='account-item login-btn waves-effect']//span[text()='Sign In']"))).click()
      
    • Note: You have to add the following imports :

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