1

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')

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

6 Comments

On my computer, with the driver for Firefox I need to use a wait() function, whereas with the driver for Chrome I don't. I think that with Chrome, it blocks until the page is fully loaded.
@VincentVidal ,Yes but in above question user uses Firefox driver
@Himanshudua Thanks a lot! I was able to successfully login to the website.
@VincentVidal, Thank you for mentioning the difference between Chrome and Firefox drivers. It's a great thing to know.
I don't think either browser behaves differently in blocking until the page is loaded. In my experience Chrome is just faster and is less likely to need a wait like Firefox or IE.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.