Search code examples
pythonseleniumxpathcss-selectorswebdriverwait

Python Selenium: Unable to locate element with id


I'm trying to select a button but I keep getting the error of:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="loginbutton"]"}
  (Session info: chrome=103.0.5060.134)

the id of the button is correct, not sure what i'm doing wrong.

I'm running this code

chrome_options = webdriver.ChromeOptions()
chrome_options.headless = False
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable-automation"])

driver = webdriver.Chrome(options=chrome_options)

def initializedBrowser():
driver.get("https://www.facebook.com/")
time.sleep(3)
initializedBrowser()

emailelement = driver.find_element(By.ID, 'email')
emailelement.send_keys("testing")

logginButton = driver.find_element(By.ID, 'loginbutton')
logginButton.click()

But i see it exist here in this image enter image description here


Solution

  • Try:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "loginbutton"))).click()
    

    You will also need the following imports:

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