Getting the error of invalid slection: Unable to locate an element with the xpath expression as following xpath is in valid.
private @FindBy(xpath = "//h1[contains(text(), 'Discover World')]")
WebElement elementSearch;
I even tried with using normalize-space, but with no luck. The action is simple, extract and test out whether the element assigned with the xpath above isDisplayed via Boolean.
The method with the implementation:
public Boolean elementText(WebElement element) {
getDriver().findElement(By.xpath(String.valueOf(element)));
System.out.println("Status of Element present " element.isDisplayed());
return null;
}
The method calling this implementation:
public void verifyPageLaunched() {
elementText(elementSearch);
}
All actions results in invalid xpath expression error only when running the test on the browser but while manually checking the xpath on browser 'inspect' - it trigger's the right element. Also checked but the above element is not within any iframe either.
Any suggestions what could be triggering this? Thanks team
CodePudding user response:
Here:
public Boolean elementText(WebElement element) {
getDriver().findElement(By.xpath(String.valueOf(element)));
System.out.println("Status of Element present " element.isDisplayed());
return null;
}
You are passing an object of type WebElement.
Then you trying to apply String.valueOf(element) on that element object of type WebElement.
So, it clearly will not work!
WebElement element is not a number, like int so you could apply String.valueOf(element) on it!
That's why the result of String.valueOf(element) is definitely NOT a valid XPath expression!
UPD
Instead of what you are doing you can do any of the following:
- Passing a
WebElementobject
public Boolean isElementDisplayed(WebElement element) {
System.out.println("Status of Element present " element.isDisplayed());
return element.isDisplayed();
}
- Passing a
By
public Boolean isElementDisplayed(By by) {
boolean displayed = getDriver().findElement(by).isDisplayed();
System.out.println("Status of Element present " displayed);
return displayed;
}
- Passing XPath String expression
public Boolean isElementDisplayed(String xpath) {
boolean displayed = getDriver().findElement(By.xpath(xpath)).isDisplayed();
System.out.println("Status of Element present " displayed);
return displayed;
}
