Reputation: 598
I have an if statement below which is getting me an issue. If certain selections are made in a different dropdown, the page displays a second dropdown and a check box. The below code works as expected when a selection is made that causes those two elements to display but it doesn't if a selection is made that doesn't make them display. I get the no such element: Unable to locate element
error. At first I thought it was returning true either way but the issue is it's crashing because. I even added a check at trying to assign the value to a booolean but still get the same error.
boolean dropdown = driver.findElement(By.id("DROPDOWN")).isDisplayed();
gets the same error.
if(driver.findElement(By.id("DROPDOWN")).isDisplayed()){
driver.findElement(By.id("DROPDOWN")).click();
driver.findElement(By.xpath("Choice in Drop DOWN)).click();
driver.findElement(By.id("CheckBox")).click();
}
Upvotes: 0
Views: 4135
Reputation: 11
This has to do with element being present in DOM verses element being visible at UI. Instead, you should use findElements. As when you save a list and then use the condition to make sure it is among that list of found elements or not, it will not produce the hard exception error.
List<WebElement> ele=driver.findElements(by.xpath"abcd///abcd");
if(ele.size()== 0){
// empty list if no matching element
System.out.println("Element not present, appearing "+ele.size()+ " time");
} else {
System.out.println("Element present, appearing "+ele.size()+ " time");
}
Upvotes: 0
Reputation: 21
isDisplayed() will work if the element is present in the DOM, followed by the style attribute :- display should not be false or none.
If prior action is a selection which led both the element to be displayed, it means the element is in the DOM but that wont be visible. So checking the visibility condition would return u false.
Try waiting for the element to get visible and perform the check operation on it which would reduce the sync latency.
WebDriverWait wait = new WebDriverWait(WebDriverRunner.getWebDriver(),5); wait.until(ExpectedConditions.visibilityOfElementLocated("By Locator"));
if (dropdown.isDisplayed())
`````````// If the dropdown is tagged with <Select> tag
``````````` Select dropDown = new Select(dropdown);
```````````dropDown .selectByValue("value);
```````` // Else fetch the dropdown list item and store it in a list and iterate through and perform the desired action
```````````List<WebElement> dropDownList = new ArrayList<Webelements>;
```````````dropDownList.forEach(element -> {
```````````if(element.getText().equals("value")){
``````` ````element.click();
``````````` }
``````````` });
```````````driver.findElement(By.id("CheckBox")).click();
}
Upvotes: 1
Reputation: 71
The following answers explain how to handle checking an element exists and handle the exception by wrapping in a custom method.
How to check if Element exists in c# Selenium drivers
I would also recommend re-writing your code as the following to avoid duplication and avoid xpath selectors. Using findElement twice for in the same context is not necessary just create a variable.
var dropdown = driver.findElement(By.id("DROPDOWN"));
if (dropdown.Displayed())
{
var selectElement = new SelectElement(dropdown);
selectElement.SelectByValue("valuehere");
}
If you are using the text rather than the value in the select box you can use SelectByText("texthere"); instead of SelectByValue.
Upvotes: 1
Reputation: 1938
findElement method will throw this hard exception - No such element if the element is not found. Just include Exception Handling for No Such Element and your logic should work just fine.
try{
if(driver.findElement(By.id("DROPDOWN")).isDisplayed()){
driver.findElement(By.id("DROPDOWN")).click();
driver.findElement(By.xpath("Choice in Drop DOWN)).click();
driver.findElement(By.id("CheckBox")).click();
}
catch (NoSuchElementException e)
{
// I believe you dont have to do anything here. May be a console log will do.
}
Upvotes: 2