user3918496
user3918496

Reputation: 55

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()

Upvotes: 1

Views: 2270

Answers (3)

Eknath
Eknath

Reputation: 644

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()

Upvotes: 1

Phung Duy Phong
Phung Duy Phong

Reputation: 896

You can find the login button by title:

submit = driver.find_element_by_xpath('//button[contains(@title, "Login")]')
submit.click()

OR You can find the form and then submit (find base on class):

submit_form = driver.find_element_by_xpath('//form[starts-with(@class, "ng-valid ng-dirty")]')
submit_form .submit()

Hope this help.

Upvotes: 0

Guy
Guy

Reputation: 50864

The xpath you are using is wrong, the correct one is

'/html/body/div[6]/div/div/div[2]/form/div[4]/button'

As a side note, you really shouldn't use absolute xpath, for example you can use '//button[@title="Login"]' for the login button.

Upvotes: 0

Related Questions