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
'indiv[@class='AppWrapper].'and"and not, incidentally, the char `