1

I have tried all possibe ways to click a button by specifying it's xpath and also by creating my own, but it doesn't seem to work. I've reffered to this

from selenium import webdriver
driver = webdriver.Chrome("/path_for_chromedriver_")
driver.get("https://www.hackerrank.com/login")

username = driver.find_element_by_xpath('//*[@id="input-1"]')
username.send_keys('#MY EMAIL')

password = driver.find_element_by_xpath('//*[@id="input-2"]')
password.send_keys('#MY PASSWORD')

could someone give me the code for finding and clicking this button with selenium

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • use `submit` method instead of `click` . `driver.find_element_by_css_selector(".ui-btn-primary.auth-button").submit` – Nik Dec 20 '19 at 10:15

2 Answers2

0

Use the following xpath to click on Log In button.

//button[.//span[text()='Log In']]

Code:

from selenium import webdriver
driver = webdriver.Chrome("/path_for_chromedriver_")
driver.get("https://www.hackerrank.com/login")

username = driver.find_element_by_xpath('//*[@id="input-1"]')
username.send_keys('#MY EMAIL')

password = driver.find_element_by_xpath('//*[@id="input-2"]')
password.send_keys('#MY PASSWORD')

driver.find_element_by_xpath("//button[.//span[text()='Log In']]").click()

Or Induce WebDriverWait() and element_to_be_clickable() and following XPath

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[.//span[text()='Log In']]"))).click()

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
KunduK
  • 26,790
  • 2
  • 10
  • 32
0

To send character sequence to the Username and Password field and click() on Log In button you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.get("https://www.hackerrank.com/auth/login")
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.input[name='username']"))).send_keys("masterchief01")
    driver.find_element_by_css_selector("input.input[name='password']").send_keys("masterchief01")
    driver.find_element_by_css_selector("button[data-analytics='LoginPassword'] span.ui-text").click()
    
  • Using XPATH:

    driver.get("https://www.hackerrank.com/auth/login")
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@class='input' and @name='username']"))).send_keys("masterchief01")
    driver.find_element_by_xpath("//input[@class='input' and @name='password']").send_keys("masterchief01")
    driver.find_element_by_xpath("//button[@data-analytics='LoginPassword']//span[@class='ui-text']").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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217