WebDriver: check if an element exists?
You could alternatively do:
driver.findElements( By.id("...") ).size() != 0
Which saves the nasty try/catch
p.s.
Or more precisely by @JanHrcek here
!driver.findElements(By.id("...")).isEmpty()
Test if element is present using Selenium WebDriver?
Use findElements
instead of findElement
.
findElements
will return an empty list if no matching elements are found instead of an exception.
To check that an element is present, you could try this
Boolean isPresent = driver.findElements(By.yourLocator).size() > 0
This will return true if at least one element is found and false if it does not exist.
The official documentation recommends this method:
findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.
How to check if element is present using Selenium with Javascript?
If you deleted the deleteButton
element it's no longer available / present, so .isDisplayed()
throwing a StaleElementReferenceError
is expected.
You can re-search for the id 'buttonId' in a try / catch block catching any exceptions and base your assertion on that.
Something like:
let buttonIsStillThere;
try {
buttonIsStillThere = driver.findElement(By.id('buttonId')).isDisplayed();
} catch () {
buttonIsStillThere = false;
}
Selenium check if element exists without exception
Instead of driver.find_element
you should use driver.find_elements
method here.
Something like this:
if driver.find_elements_by_xpath("/div[@class='class_name']"):
driver.find_element_by_xpath("/div[@class='class_name']").click()
Or this:
elements = driver.find_elements_by_xpath("/div[@class='class_name']")
if elements:
elements[0].click()
driver.find_elements
will return you a list of web elements matching the passed locator. In case such elements found it will return non-empty list interpreted by Python as a Boolean True
while if no matches found it will give you an empty list interpreted by Python as a Boolean False
How do I determine if a WebElement exists with Selenium?
Best practice is to do what you originally suggested, use .findElements()
and check for .size != 0
or you can also use my preference, .isEmpty()
. You can create a Util function like the below to test if an element exists.
public boolean elementExists(By locator)
{
return !driver.findElements(locator).isEmpty();
}
You could also build this into a function in your page object.
How to check one element is present inside another element in Selenium Webdriver with Java?
WebElement first = driver.findElement(By.xpath(".//*[@class='width-90px']"));
first.findElement(By.xpath(".//*[@class='fa fa-check font-14 ']")).isDisplayed();
is working fine, if second element is not present inside first element its throwing error.
Selenium ignore if element not present
Two ways to fix it
Use
findElements
that would return alist of web element
if found otherwisesize
oflist
will be0
try {
if (driver.findElements(By.cssSelector(".arrow-icon ")).size() > 0 ) {
System.out.println("Size is > 0 so there must be at least one web element, do some interaction below like click etc");
new WebDriverWait(driver, 40).until(ExpectedConditions.elementToBeClickable(By.cssSelector(".arrow-icon "))).click();
}
else {
System.out.println("Size is 0, so web element must not be present, do something here that would make it available");
}
}
catch (Exception e) {
// TODO: handle exception
}Use risky code inside directly
try
try {
new WebDriverWait(driver, 40).until(ExpectedConditions.elementToBeClickable(By.cssSelector(".arrow-icon "))).click();
}
catch (Exception e) {
// TODO: handle exception
}
Related Topics
How to Understand Happens-Before Consistent
Is Java.Time Failing to Parse Fraction-Of-Second
How to Add and Remove Elements of Enumeration at Runtime in Java
How to Get Pid of Process I'Ve Just Started Within Java Program
MAC Os X and Multiple Java Versions
Java Replace Line in Text File
Where's My Invalid Character (Ora-00911)
Convert Rgb Values to Color Name
How to Read Jsch Command Output
Integer Wrapper Class and == Operator - Where Is Behavior Specified
Java - Transparent Jscrollpane
Why Double Width = 50/110000; the Output Is 0.000000000000000
Why Does Java Implicitly (Without Cast) Convert a 'Long' to a 'Float'
Android "Gps Requires Access_Fine_Location" Error, Even Though My Manifest File Contains This
How to Scroll a Scrollview Programmatically in Android