1

I have just started using Selenium for automation testing and have been following a tutorial via udemy.

I have found the xpath to a navbar once the user has logged in to ensure that the person has logged in. Have tested it in the console and it can find the navbar. Set it up in my script and it returns a Syntax error.

Here is the html to the navbar that I am trying to locate:

<ui-view class="ng-scope">
  <app-route >
    <div class="AppWrapper">
      <app-header class="ng-isolate-scope">
        <header class="AppHeader">
          <nav class="NavBar">

My xpath code:

WebElement navBar = driver.findElement(By.xpath("//ui-view[@class='ng-scope']/app-route/div[@class='AppWrapper]/app-header[@class='ng-isolate-scope']//nav[@class='NavBar']"));

Error:

[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 13.796 s <<< FAILURE! - in com.testing.autoTest
[ERROR] com.testing.autoTest.loginTest  Time elapsed: 8.387 s  <<< FAILURE!
org.openqa.selenium.InvalidSelectorException: 
invalid selector: Unable to locate an element with the xpath expression //ui-view[@class='ng-scope']/app-route/div[@class='AppWrapper]/app-header[@class='ng-isolate-scope']//nav[@class='NavBar'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//ui-view[@class='ng-scope']/app-route/div[@class='AppWrapper]/app-header[@class='ng-isolate-scope']//nav[@class='NavBar']' is not a valid XPath expression.

Console test:

$x("//ui-view[@class='ng-scope']/app-route/div[@class='AppWrapper']/app-header[@class='ng-isolate-scope']//nav[@class='NavBar']")
[nav.NavBar]0: nav.NavBarlength: 1__proto__: Array(0)

I have broken the xpath down to incremental chunks starting with //ui-view[@class='ng-scope'] and slowly adding more to it and it seems that once it gets to //ui-view[@class='ng-scope']/app-route/div[@class='AppWrapper] that is where the test fails.

I have also tried using other elements on the page and it breaks at the same point.

Any tips would be greatly appreciated thank you.

EDIT: Typo as pointed out by zx485 that lead to a new erorr:

[ERROR] com.testing.autoTest.loginTest  Time elapsed: 9.271 s  <<< FAILURE!
org.openqa.selenium.NoSuchElementException: 
no such element: Unable to locate element: {"method":"xpath","selector":"//ui-view[@class='ng-scope']/app-route/div[@class='AppWrapper']/app-header[@class='ng-isolate-scope']//nav[@class='NavBar']"}

EDIT 2: I found the issue with the second error, the test was trying to run too quickly before the elements had loaded so added a sleep(3000); timer before looking for the navbar and the test passed

3
  • It's just a typo: you forgot the closing ' in div[@class='AppWrapper]. Commented Jul 26, 2021 at 21:09
  • Opps! Thanks for that! Now I am getting another error. Will update my original post Commented Jul 26, 2021 at 21:13
  • Take care that you use only ' and " and not, incidentally, the char ` Commented Jul 26, 2021 at 21:14

1 Answer 1

1

As mentioned in the comment you have a type - missing closing ' in div[@class='AppWrapper].
However I would suggest using this locator:

//nav[@class='NavBar']

This should be unique enough.

Sign up to request clarification or add additional context in comments.

Comments

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.