I am very new to Selenium.
I try to build a test that will validate whether web element (small icon) is shown on the page.
So, on the top of my class I defined Web element, by xpath.
Xpath I copied from Inspect after applying right click on the element and clicking Inspect.
@FindBy(xpath = "//*[@id="referrals"]/tbody/tr[2]/td[2]/div/img[2]") private WebElement ChainAndTwoArrowsIcon;
Then in the same class I have a method that evaluates whether my icon is appears:
public boolean IconChainWithArrowIsFound() throws InterruptedException {
return ChainAndTwoArrowsIcon.isDisplayed()
&& ChainAndTwoArrowsIcon.getAttribute("title").toString().contains("Associated, and was successfully sent.");
}
This is the last bit of my code and all xpath references I used in the same code worked fine same about all of the code.
However, I am getting an error at the end of the run for this bit I described:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="referrals"]/tbody/tr[2]/td[2]/div/img[2]"}
Please, see the image attached
Can I have an advice what could potentially be wrong? Thank you in advance.
CodePudding user response:
Please check if the page has frames/iframes and the element you are trying to find is inside a frame/iframe. If so, you will need to first switch to the said frame using driver.switchTo().frame('frameName')
and then you should be able to locate your element.
CodePudding user response:
isDisplayed()
will result in NoSuchElementException
if Element is not found. if found it would work expected.
Basically, you'd never be sure if it works or not, cause it is directly dependent on element locator which is not a best practices.
There are two ways to handle this situation
- Use
findElements
instead :
Code :
@FindBy(xpath = "//*[@id='referrals']/tbody/tr[2]/td[2]/div/img[2]")
private List<WebElement> ChainAndTwoArrowsIcon;
public boolean IconChainWithArrowIsFound() throws InterruptedException {
boolean flag = false;
List<WebElement> elements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElements(ChainAndTwoArrowsIcon));
if (elements.size()>0) {
System.out.println("Element is visible");
ChainAndTwoArrowsIcon.get(0).getAttribute("title").toString().contains("Associated, and was successfully sent.");
return flag = true;
}
else {
System.out.println("Element is not visible");
return flag;
}
}
Basically, findElements
will return a list if element is found, if not then we will have empty list. Based on size of list we have derived the if and else above.
Use try-catch
Code :
@FindBy(xpath = "//*[@id='referrals']/tbody/tr[2]/td[2]/div/img[2]")
private WebElement ChainAndTwoArrowsIcon;
public boolean IconChainWithArrowIsFound() throws InterruptedException {
try {
return ChainAndTwoArrowsIcon.isDisplayed()
&& ChainAndTwoArrowsIcon.getAttribute("title").toString().contains("Associated, and was successfully sent.");
}
catch (Exception e) {
System.out.println("Something went wrong.");
e.printStackTrace();
return false;
}
}