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 correctly.
The two texts and the button are in a form without an id or some tag, also I'm 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>
Full HTML (URLS are changed) :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Login</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="shortcut icon" type="image/png" href="https://example.com/favicon.png?"/>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i,900"
rel="stylesheet">
<!-- Icon fonts -->
<link href="/fonts/fontawesome-pro/css/all.min.css" rel="stylesheet" type="text/css"/>
<!-- theme -->
<link id="theme-link" href="https://example.com/theme.css?&ver=a_230wednesday_10_nov_2021_135539_utc" rel="stylesheet" type="text/css"/>
<link href="https://example.com/login.css?&ver=a_230wednesday_10_nov_2021_135539_utc" rel="stylesheet" type="text/css"/>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag( 'js', new Date() );
gtag( 'config', "UA-101862321-1" );
</script>
</head>
<body>
<div class="limiter d-none">
<div class="container-login100">
<div class="wrap-login100 align-content-center align-items-center">
<div class="login100-pic js-tilt" data-tilt>
<img src="https://example.com/login-illustration.png?"/>
</div>
<div class="login100-form">
<div class="text-center mb-3">
<img src="https://example.com/logo-login.png?"/>
</div>
<div class="card-body p-4">
<form action="https://www.example.com/login" autocomplete="on" method="post" role="form">
<input type="hidden" name="_token" value="Somevalue">
<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">
<label class="custom-control custom-checkbox">
<input id="remember" name="remember" type="checkbox" class="custom-control-input" value="1">
<span class="custom-control-label">Remember me</span>
</label>
</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>
<div class="d-flex align-items-center form-group">
<hr class="flex-grow-1"/>
<div class="text-muted px-3">
OR CONNECT WITH
</div>
<hr class="flex-grow-1"/>
</div>
<a href="https://example.com/login/office" class="btn btn-default text-center btn-block">
<img src="https://example/office-365.png?&ver=a_230wednesday_10_nov_2021_135539_utc" class="img-fluid"/>
</a>
<div class="text-right mt-3">
<a href="https://www.example.com/password/reset">
Forgot Your Password?
</a>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="login-cover-bg">
<div class="login-box d-flex justify-content-between align-items-center">
<div class="login100-pic">
<img src="https://example.com/logo-login.png?"/>
</div>
<div class="login100-form">
<div class="text-center mb-3">
<img src="https://example.com/logo-login.png?" class="img-fluid"/>
</div>
<div class="card-body p-4">
<form action="https://www.example.com/login" autocomplete="on" method="post" role="form">
<input type="hidden" name="_token" value="somevalue">
<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">
<label class="custom-control custom-checkbox">
<input id="remember" name="remember" type="checkbox" class="custom-control-input" value="1">
<span class="custom-control-label">Remember me</span>
</label>
</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>
<div class="d-flex align-items-center form-group">
<hr class="flex-grow-1"/>
<div class="text-muted px-3">
OR CONNECT WITH
</div>
<hr class="flex-grow-1"/>
</div>
<a href="https://www.example.com/login/office" class="btn btn-default text-center btn-block">
<img src="office-365.png?&ver=a_230wednesday_10_nov_2021_135539_utc" class="img-fluid"/>
</a>
<div class="text-right mt-3">
<a href="https://www.example.com">
Forgot Your Password?
</a>
</div>
</div>
</div>
</div>
</div>
<!-- Core scripts From Bootstrap 4.4-->
<script src="https://code.jquery.com/jquery-3.4.1.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" ></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" "></script>
</body>
</html>
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('random@random.com')
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)
Thank you in advance!