I'm trying to auto login to a website. When the main page loads, It has a login button, which brings up the login form with username
and password
forms.
The button is inside a div
element with an id equal to ' loginbox
':
<div class="col-md-6 col-sm-4 text-right" id="loginbox">
<div class="row">
<div class="col-md-12 margin-bottom-10">
<button type="button" class="btn btnlogin" data-toggle="modal" data-
target="#loginDiv">
This is my code for finding the button, and then clicking it:
driver = webdriver.Firefox()
driver.get('websiteURL')
wait = WebDriverWait(driver, 10)
driver.find_element_by_xpath("//div[@id='loginbox']/button[1]").click()
When I run the code, I get this error:
"Unable to locate element: //div[@id='loginbox']/button[1]"
How should I locate this button?
This is the rest of my code for filling username
and password
inputs:
username = wait.until(EC.visibility_of_element_located((By.NAME, "FRMLoginUname")))
username.clear()
username.send_keys('UserName')
password = wait.until(EC.visibility_of_element_located((By.NAME, "FRMLoginPassword")))
password.clear()
password.send_keys('Password')
The problem occurs because loginbox
is loaded by ajax or jquery. You should use selenium Waits. There is two type of waits explicit and implicit. Avoid using implicit wait.
# Explicit wait example
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element_login_box = wait.until(EC.element_to_be_clickable((By.ID, 'loginbox')))
# implicit wait example
driver.implicitly_wait(10) # seconds
driver.find_element_by_xpath("//div[@id='loginbox']/button[1]").click()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.