Dan
Dan

Reputation: 185

Python Selenium: Difficulty finding the password element

I am attempting to use Selenium for the first time to login to a website in automated fashion. I am able to locate the email (serving as a username) element pretty easily using this code:

enter image description here

However, for the password, there are no id or name tags, so I'm having difficulty finding that element. Here is the source code of the page. The password element is highlighted in gray below:

enter image description here

I have tried locating the password element by link_text and class_name with the following statements, but both have failed:

enter image description here

I imagine locating by XPath may be the way to go here, but I am unsure of the syntax (especially since there are so many div tags). Any assistance is appreciated.

Upvotes: 0

Views: 2133

Answers (3)

Void Spirit
Void Spirit

Reputation: 909

The best way is

driver.findElement(By.xpath("//input[@type='password'][@title='Password']")) 

Try to use less class selectors and use ids or tags. Classes can be associated with another elements. This is in Java, sorry. You can find it in python if you need it.

Upvotes: 0

jhylands
jhylands

Reputation: 1014

One way to do this assuming there are no other password elements on the page would be to find all the input tags and then filter them to the ones that are password fields.

def getPassword(driver):
    inputs = driver.find_elements_by_tag_name('input')
    for input in inputs:
        if input.get_attribute('type')=='password':
            return input
    return None

Got syntax help from this question

Upvotes: 0

Guy
Guy

Reputation: 50864

request.password is the ng-model attribute, not text.

find_element_by_class_name receives one class, you tried with three.

Use css_selector to locate the element:

By request.password

driver.find_element_by_css_selector('[ng-model="request.password"]')

By the classes

driver.find_element_by_css_selector('.form-control.ng-pristine.ng-valid')

Upvotes: 2

Related Questions