Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WebElement getStaleElemById(String id, WebDriver driver) {
- try {
- return driver.findElement(By.id(id));
- } catch (StaleElementReferenceException e) {
- System.out.println("Attempting to recover from StaleElementReferenceException ...");
- return getStaleElemById(id, driver);
- }
- }
- WebElement getStaleElemByCss(String css, WebDriver driver) {
- try {
- return driver.findElement(By.cssSelector(css));
- } catch (StaleElementReferenceException e) {
- System.out.println("Attempting to recover from StaleElementReferenceException ...");
- return getStaleElemByCss(css, driver);
- } catch (NoSuchElementException ele) {
- System.out.println("Attempting to recover from NoSuchElementException ...");
- return getStaleElemByCss(css, driver);
- }
- }
- public class NeverStaleWebElement implements WebElement {
- private WebElement element;
- private final WebDriver driver;
- private final By foundBy;
- public NeverStaleWebElement(WebElement element, WebDriver driver, By foundBy) {
- this.element = element;
- this.driver = driver;
- this.foundBy = foundBy;
- }
- @Override
- public void click() {
- try {
- element.click();
- } catch (StaleElementReferenceException e) {
- // log exception
- // assumes implicit wait, use custom findElement() methods for custom behaviour
- element = driver.findElement(foundBy);
- // recursion, consider a conditioned loop instead
- click();
- }
- }
- // ... similar for other methods, too
- }
- WebElement getStaleElem(By by, WebDriver driver) {
- try {
- return driver.findElement(by);
- } catch (StaleElementReferenceException e) {
- System.out.println("Attempting to recover from StaleElementReferenceException ...");
- return getStaleElem(by, driver);
- } catch (NoSuchElementException ele) {
- System.out.println("Attempting to recover from NoSuchElementException ...");
- return getStaleElem(by, driver);
- }
- }
- WebElement getStaleElem(By by, WebDriver driver) {
- try {
- return driver.findElement(by);
- } catch (StaleElementReferenceException | NoSuchElementException e) {
- System.out.println("Attempting to recover from " + e.getClass().getSimpleName() + "...");
- return getStaleElem(by, driver);
- }
- }
- boolean isStillOnOldPage = true;
- while (isStillOnOldPage) {
- try {
- theElement.getAttribute("whatever");
- } catch (StaleElementReferenceException e) {
- isStillOnOldPage = false;
- }
- }
- WebDriverWait wait = new WebDriverWait(driver, 15);
- wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("theElementId")));
- public int getNumberOfElementsFound(By by) {
- return driver.findElements(by).size();
- }
- public WebElement getElementWithIndex(By by, int pos) {
- return driver.findElements(by).get(pos);
- }
- /**click on each link */
- public void getLinks()throws Exception{
- try {
- List<WebElement> componentList = driver.findElements(By.tagName("a"));
- System.out.println(componentList.size());
- for (WebElement component : componentList)
- {
- //click1();
- System.out.println(component.getAttribute("href"));
- }
- int numberOfElementsFound = getNumberOfElementsFound(By.tagName("a"));
- for (int pos = 0; pos < numberOfElementsFound; pos++) {
- if (getElementWithIndex(By.tagName("a"), pos).isDisplayed()){
- getElementWithIndex(By.tagName("a"), pos).click();
- Thread.sleep(200);
- driver.navigate().back();
- Thread.sleep(200);
- }
- }
- }catch (Exception e){
- System.out.println("error in getLinks "+e);
- }
- }
- driver = webdriver.Firefox();
- driver.get("http://www.github.com");
- search_input = lambda: driver.find_element_by_name('q');
- search_input().send_keys('hello worldn');
- time.sleep(5);
- search_input().send_keys('hello frankn') // no stale element exception
- # Using Jquery queue to get animation queue length.
- animationQueueIs = """
- return $.queue( $("#%s")[0], "fx").length;
- """ % element_id
- wait_until(lambda: self.driver.execute_script(animationQueueIs)==0)
- self.driver.execute_script("$("li:contains('Narendra')").click()");
- # Wait till the element goes stale, this means the list has updated
- wait_until(lambda: is_element_stale(old_link_reference))
- for(int j=0; j<5;j++)
- try {
- WebElement elementName=driver.findElement(By.xpath(“somexpath”));
- break;
- } catch(StaleElementReferenceException e){
- e.toString();
- System.out.println(“Stale element error, trying :: ” + e.getMessage());
- }
- elementName.sendKeys(“xyz”);
Add Comment
Please, Sign In to add comment