How to check if an object is visible in a webpage by using its xpath?
To check if an element exists or not, use findElements()
method. It would return an empty list if no element matching a locator found - an empty list is "falsy" by definition:
if (length(remDr$findElements(using='xpath', x_path))!=0) { print("Logo Exists")}
To check if an element is visible, use isElementDisplayed()
:
webElems <- remDr$findElements(using='xpath', x_path)if (webElems) { webElem <- webElems[0] if (webElem$isElementDisplayed()[[1]]) { print("Logo is visible") } else { print("Logo is present but not visible") }} else { print("Logo is not present")}
To check for presence, alternatively and instead of findElements()
, you can use findElement()
and handle NoSuchElement
exception.