Search code examples
pythonseleniumselenium-webdriverauthenticationnosuchelementexception

Python, Selenium... not able to find an element which is obviously there


I am trying to use Python (Selenium) to extract data from this site: https://sin.clarksons.net/

After I put in user name and password, it is not able to click the obvious "Submit" bottom. Can some of you help to see why? TIA.

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

if __name__ == "__main__":
    try:
        chrome_path = r"C:\Users\xxx\Downloads\chromedriver_win32\chromedriver.exe"
        driver = webdriver.Chrome(chrome_path)
        driver.get("https://www.clarksons.net/")
        driver.maximize_window()
        time.sleep(5)

        login = driver.find_element_by_xpath('//*[@id="menu"]/li[1]/span')
        time.sleep(5)
        login.click()
        time.sleep(5)

        username = driver.find_element_by_xpath('//input[@id="usernameText"]')
        username.clear()
        username.send_keys("abc(at)hotmail.com")

        password = driver.find_element_by_xpath('/html/body/div[6]/div/div/div[2]/form/div[2]/div/input[1]')
        password.clear()
        password.send_keys("xyzabc")

        submit = driver.find_element_by_xpath('/html/body/div[6]/div/div/div[2]/form/div[4]/div/div/button')
        submit.click()
        time.sleep(5)
        print "login"
        driver.quit()
    except Exception as e:
        print e
        driver.quit()

Solution

  • Try this replace xpath with id and use css selector for login button

        username = driver.find_element_by_id("usernameText")
        username.clear()
        username.send_keys("[email protected]")
    
        password = driver.find_element_by_id("passwordText")
        password.clear()
        password.send_keys("xyzabc")
    
        #submit = driver.find_element_by_xpath(".//button[@title='Login']")
        submit = driver.find_element_by_css_selector("#home button.btn-
        primary")
        submit.click()