0

I am writing on a selenium program to scrape a site that is behind a login. However everything except the Placeholder text is dynamic including the css selectors, the xpath etc. Is there a way to select by visible text without Xpath?

html snippet:

<form class="un-form un-login-form "><div class="un-login-form__change-school form-group">Name<br><small>D-26871, Russellstr. 33</small><br><a>Schule wechseln</a></div><div class="form-group"><input type="text" value="" placeholder="Benutzer" id="732de780-8f7b-41f6-98ba-c73503c93fcc" class="form-control"></div><div class="form-group"><input type="password" value="" placeholder="Passwort" id="5d7bc08a-7301-4348-82f1-a16e9e657720" class="form-control"></div><div class="un-login-buttons un-login-form__buttons btn-group"><button type="submit" title="Login" class="btn btn-primary">Login</button></div><div class="un-login-form__links"><a>Passwort vergessen?</a><br></div></form>

my code:

paswd = driver.find_element_by_xpath("//input[@placeholder='Passwort']")

So due to the variing xpath i always get the error not found also due to the classes all named the same it cant focus the element

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
3 agle
  • 1
  • 1
  • 4

3 Answers3

0

As the id is dynamic you can use the other attributes and you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("input.form-control[placeholder='Passwort']").send_keys("3agle")
    
  • Using xpath:

    driver.find_element_by_xpath("//input[@class='form-control' and @placeholder='Passwort']").send_keys("3agle")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • so i still get the same error sadly. just to provide an idea this is the full xpath for chrome: //*[@id="app"]/div/div[1]/div/section/section/div/div[1]/div/div[2]/div[1]/div[2]/form/div[2] now this is the xpath of one of my sessions for form-group. but this changes each time next session was: //*[@id="app"]/div/div[1]/div/section/section/div/div[1]/div/div[2]/div[1]/div[2]/form/div[2] just as an example so the first one was //*[@id="app"]/div/div[1]/div/section and the second was //*[@id="app"]/div/div[1]/div/section/ – 3 agle Jun 26 '19 at 14:58
0

Please try driver.findElement(By.tagName("")

Arun Nair
  • 425
  • 3
  • 11
0

ok now this kind of XPATH did it for me: //*[@placeholder='Benutzer']

3 agle
  • 1
  • 1
  • 4
  • oh and weirdly only works if i go with my mouse over the username input and drag the window than hold it clicked until login succeeded – 3 agle Jun 27 '19 at 14:31