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
CodePudding user response:
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
CodePudding user response:
The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#loginbutton[name='login']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='loginbutton' and @name='login']"))).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