0

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.

Guy
  • 46,488
  • 10
  • 44
  • 88
Dan
  • 165
  • 5
  • 18
  • 1
    Please post html and code as text, not as images. – Guy Oct 02 '18 at 08:38
  • There is a easy way to get the XPATH with google chrome, in the developer tools just click the element with the right click and select copy XPATH, – Gil Sousa Oct 02 '18 at 08:42
  • Thanks! I was able to do this using xpath after copying the xpath in the developer tools. – Dan Oct 02 '18 at 09:21

3 Answers3

2

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')
Guy
  • 46,488
  • 10
  • 44
  • 88
0

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

jhylands
  • 984
  • 8
  • 16
0

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.

Void Spirit
  • 879
  • 6
  • 18