When looking at a dynamic element on a webpage, Selenium crashes if the element is not present. As a result I'm having to rescue the application to continue. I figure I'm doing something wrong with the syntax
response = driver.find_element(:class, element).text
If the element is not found, Selenium errors and crashes the application. This happens regardless of my browser configuration.
CodePudding user response:
Selenium is not crashing. Your code has encountered an exceptional condition (attempting to work with an element that is not there). The code is correctly responding to that exceptional condition by raising a NoSuchElementError
.
If you are trying to determine if an element is there, you can use Driver#find_elements
and check if the Array#size
equals 0.
If you are trying to work with an element that is not yet on the page, you'll need to create an explicit wait to poll for the element to show up as in DebanjanB's answer.
CodePudding user response:
You need to wait a bit for the element to be visible before you try to locate it as follows:
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
ele = wait.until { driver.find_element(:class, element).displayed? }
response = ele.text