Guest User

Untitled

a guest
Jun 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. WebElement getStaleElemById(String id, WebDriver driver) {
  2. try {
  3. return driver.findElement(By.id(id));
  4. } catch (StaleElementReferenceException e) {
  5. System.out.println("Attempting to recover from StaleElementReferenceException ...");
  6. return getStaleElemById(id, driver);
  7. }
  8. }
  9.  
  10. WebElement getStaleElemByCss(String css, WebDriver driver) {
  11. try {
  12. return driver.findElement(By.cssSelector(css));
  13. } catch (StaleElementReferenceException e) {
  14. System.out.println("Attempting to recover from StaleElementReferenceException ...");
  15. return getStaleElemByCss(css, driver);
  16. } catch (NoSuchElementException ele) {
  17. System.out.println("Attempting to recover from NoSuchElementException ...");
  18. return getStaleElemByCss(css, driver);
  19. }
  20. }
  21.  
  22. public class NeverStaleWebElement implements WebElement {
  23. private WebElement element;
  24. private final WebDriver driver;
  25. private final By foundBy;
  26.  
  27. public NeverStaleWebElement(WebElement element, WebDriver driver, By foundBy) {
  28. this.element = element;
  29. this.driver = driver;
  30. this.foundBy = foundBy;
  31. }
  32.  
  33. @Override
  34. public void click() {
  35. try {
  36. element.click();
  37. } catch (StaleElementReferenceException e) {
  38. // log exception
  39.  
  40. // assumes implicit wait, use custom findElement() methods for custom behaviour
  41. element = driver.findElement(foundBy);
  42.  
  43. // recursion, consider a conditioned loop instead
  44. click();
  45. }
  46. }
  47.  
  48. // ... similar for other methods, too
  49.  
  50. }
  51.  
  52. WebElement getStaleElem(By by, WebDriver driver) {
  53. try {
  54. return driver.findElement(by);
  55. } catch (StaleElementReferenceException e) {
  56. System.out.println("Attempting to recover from StaleElementReferenceException ...");
  57. return getStaleElem(by, driver);
  58. } catch (NoSuchElementException ele) {
  59. System.out.println("Attempting to recover from NoSuchElementException ...");
  60. return getStaleElem(by, driver);
  61. }
  62. }
  63.  
  64. WebElement getStaleElem(By by, WebDriver driver) {
  65. try {
  66. return driver.findElement(by);
  67. } catch (StaleElementReferenceException | NoSuchElementException e) {
  68. System.out.println("Attempting to recover from " + e.getClass().getSimpleName() + "...");
  69. return getStaleElem(by, driver);
  70. }
  71. }
  72.  
  73. boolean isStillOnOldPage = true;
  74. while (isStillOnOldPage) {
  75. try {
  76. theElement.getAttribute("whatever");
  77. } catch (StaleElementReferenceException e) {
  78. isStillOnOldPage = false;
  79. }
  80. }
  81. WebDriverWait wait = new WebDriverWait(driver, 15);
  82. wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("theElementId")));
  83.  
  84. public int getNumberOfElementsFound(By by) {
  85. return driver.findElements(by).size();
  86. }
  87.  
  88. public WebElement getElementWithIndex(By by, int pos) {
  89. return driver.findElements(by).get(pos);
  90. }
  91.  
  92. /**click on each link */
  93. public void getLinks()throws Exception{
  94. try {
  95. List<WebElement> componentList = driver.findElements(By.tagName("a"));
  96. System.out.println(componentList.size());
  97.  
  98. for (WebElement component : componentList)
  99. {
  100. //click1();
  101. System.out.println(component.getAttribute("href"));
  102. }
  103. int numberOfElementsFound = getNumberOfElementsFound(By.tagName("a"));
  104. for (int pos = 0; pos < numberOfElementsFound; pos++) {
  105. if (getElementWithIndex(By.tagName("a"), pos).isDisplayed()){
  106.  
  107. getElementWithIndex(By.tagName("a"), pos).click();
  108. Thread.sleep(200);
  109. driver.navigate().back();
  110. Thread.sleep(200);
  111. }
  112. }
  113. }catch (Exception e){
  114. System.out.println("error in getLinks "+e);
  115. }
  116. }
  117.  
  118. driver = webdriver.Firefox();
  119. driver.get("http://www.github.com");
  120. search_input = lambda: driver.find_element_by_name('q');
  121. search_input().send_keys('hello worldn');
  122. time.sleep(5);
  123.  
  124.  
  125. search_input().send_keys('hello frankn') // no stale element exception
  126.  
  127. # Using Jquery queue to get animation queue length.
  128. animationQueueIs = """
  129. return $.queue( $("#%s")[0], "fx").length;
  130. """ % element_id
  131. wait_until(lambda: self.driver.execute_script(animationQueueIs)==0)
  132.  
  133. self.driver.execute_script("$("li:contains('Narendra')").click()");
  134.  
  135. # Wait till the element goes stale, this means the list has updated
  136. wait_until(lambda: is_element_stale(old_link_reference))
  137.  
  138. for(int j=0; j<5;j++)
  139. try {
  140. WebElement elementName=driver.findElement(By.xpath(“somexpath”));
  141. break;
  142. } catch(StaleElementReferenceException e){
  143. e.toString();
  144. System.out.println(“Stale element error, trying :: ” + e.getMessage());
  145. }
  146. elementName.sendKeys(“xyz”);
Add Comment
Please, Sign In to add comment