Home > database >  How to find a input type and submit type with selenium
How to find a input type and submit type with selenium

Time:11-11

There is an HTML page that i would like to find the elements of two input types and press one button to log in with the help of selenium along with python3. The problem is that i can't seem to find a way of doing this correct.

The two texts and the button are in a form without an id or some tag, also im new on this one.

Below there is the HTML code with the two text fields (Email & Password) i need to find with the selenium webdriver along with the submit button.

HTML CODE:

<form action="https://www.example.com/login" autocomplete="on" method="post" role="form">
<input type="hidden" name="_token" value="asdc">

<div class="form-group">
<input type="email" name="email"                                       class="form-control " placeholder="Email"
value="" autofocus>
</div>

<div class="form-group">
<input type="password" name="password" class="form-control " placeholder="Password">
</div>

<div class="form-group">
<button type="submit" class="btn btn-primary btn-block" style1="height: 41px; font-size: 18px">Sign In</button></div>
</form>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

My code so far:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)
  

driver.get("http://example.com/")


try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.LINK_TEXT, "Login to start working"))
    )
    element.click()

    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//input[@placeholder='Password']"))
    )
    element.click()
    element.send_keys('[email protected]')
    element.send_keys(Keys.ENTER)

    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "Submit"))
    )
    element.click()

    
except Exception as exc:
    driver.quit()
    print(exc)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Thank you in advance!

CodePudding user response:

I just changed your code from presence_of_element_located expected conditions to presence_of_element_located, corrected the locators and make some more things clearer. I hope now this should work.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)
  

driver.get("http://example.com/")


email_input = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='email']")))
email_input.click()
email_input.send_keys("your_email_value")

password_input = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='password']")))
password_input.click()
password_input.send_keys("your_password_value")

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//button[@type='submit']"))).click()    

CodePudding user response:

To click on the link Login to start working, next key in the credentials and finally to click on the Submit button you can use the following Locator Strategies:

driver.get("http://example.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Login to start working"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.form-group > input.form-control[name='email']"))).send_keys("[email protected]")
driver.find_element(By.CSS_SELECTOR, "div.form-group > input.form-control[name='password']").send_keys("random_password")
driver.find_element(By.XPATH, "//div[@class='form-group']/button[@class='btn btn-primary btn-block' and text()='Sign In']").click()
  • Related