0

I cannot locate err_message elements for email and password. I'm getting an error as:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"body > div:nth-child(1) > div:nth-child(4) > form:nth-child(2) > div:nth-child(1) > p:nth-child(3)"}

Tried with:

.form-signin .form_item .err_message:nth-child(1), .err_message:nth-of-type(1)

HTML snapshot:

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • you should start from form.sign-in , not body , there can be anything popping up in the structure you are not aware of – G-Cyrillus Mar 08 '23 at 21:59
  • I tried .form-signin p:nth-child(2), and it thows exception too , org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".form-signin p:nth-child(2)"} – Lena Azaryan Mar 08 '23 at 22:26
  • Please don't post [code as an image](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors). Place it directly into your question and format it as a code sample (fifth button from the left). – Brett Donald Mar 08 '23 at 22:49
  • ok I will next time, thank you – Lena Azaryan Mar 09 '23 at 06:02

2 Answers2

0

A CSS selector which starts from the body element and uses > to identify every descendant down to the target, as you have done, is not a great idea, because as soon as you move things around in your document, the selector will break. Instead, use classes or ids to target elements directly.

In your case, the only ids you have in your code are on the input fields, but as these immediately precede your error message elements, you could use the adjacent sibling combinator to target your error message elements:

The error message for the email field could be selected with any of the following:

  • #email + p
  • #email + .err_message
  • #email + p.err_message

The error message for the password field could be selected with any of the following:

  • #password + p
  • #password + .err_message
  • #password + p.err_message
Brett Donald
  • 6,745
  • 4
  • 23
  • 51
0

To locate the <p> elements you can use the Email Address and Password field as a reference identified through unique attributes and you can use the following css_selector:

  • <p> tag for Email Address field:

    WebElement element = driver.findElement(By.cssSelector("div.login_page form.form-signin div.form_item input#email +p"));
    
  • <p> tag for Password field:

    WebElement element = driver.findElement(By.cssSelector("div.login_page form.form-signin div.form_item input#password +p"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352