3
votes

I am able to enter username and password, but not able to press login button in selenium. May you advise? I didn't put the specific website, sorry.

thanks so much for your help!

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

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-infobars")
driver = webdriver.Chrome(executable_path = "C:\Python36-32\selenium\chromedriver",chrome_options=chrome_options)


driver.get("website")


elem = driver.find_element_by_name("UserName")
elem.send_keys("test")

elem = driver.find_element_by_name("Password")
elem.send_keys("test")

Above Work

Below, Not Sure Here

elem = driver.find_element_by_type("submit")
elem.click()

Outer HTML

<input value="" class="userid-button" type="submit">

Selector

#online_login > input.userid-button

XPath

//*[@id="online_login"]/input[3]

Element

<input value="" class="userid-button" type="submit">
2
You need to be more specific about what not able to press login button means and you need to post the relevant html. - Guy

2 Answers

3
votes

To click the login button you can use the following block of code:

  • xpath:

    elem = driver.find_element_by_xpath("//input[@class='userid-button'][@type='submit']")
    elem.click()
    
  • css:

    elem = driver.find_element_by_css_selector("input.userid-button[type=submit]")
    elem.click()
    
0
votes

You're on the right track. elem = driver.find_element_by_type("submit") elem.click() will only work if the login button is the only "submit" element on the page. Pages usually come with multiple "submit" elements, so it's probably not a great way to do it. The best way to do it, in my opinion, is to use the XPath or the CSSpath for the "submit" element you want. Since both XPath and CSSpath will give you a unique path for each individual element, this will find the exact "submit" button you want to click.