StaleElementReferenceException: stale element reference: element is not attached to the page document (Calendar/DatePicker)

huangapple 未分类评论54阅读模式
英文:

StaleElementReferenceException: stale element reference: element is not attached to the page document (Calendar/DatePicker)

问题

  1. package com.w2a.rough;
  2. import java.util.List;
  3. import java.util.concurrent.TimeUnit;
  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.chrome.ChromeDriver;
  8. import org.openqa.selenium.support.ui.ExpectedConditions;
  9. import org.openqa.selenium.support.ui.WebDriverWait;
  10. import io.github.bonigarcia.wdm.WebDriverManager;
  11. public class ExpediaDatePicker {
  12. public static void main(String[] args) throws InterruptedException {
  13. WebDriverManager.chromedriver().setup();
  14. WebDriver driver = new ChromeDriver();
  15. driver.manage().window().maximize();
  16. driver.manage().deleteAllCookies();
  17. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  18. WebDriverWait wait = new WebDriverWait(driver, 15);
  19. driver.get("https://www.expedia.com");
  20. // Click on the flight tab
  21. driver.findElement(By.cssSelector("#uitk-tabs-button-container > li:nth-child(2) > a")).click();
  22. // Click on Departing field to open the calendar
  23. driver.findElement(By.cssSelector("#d1-btn")).click();
  24. // Planned departure month and date
  25. String DepartMonth = "November 2020";
  26. String DepartDate = "15";
  27. // Navigate through the months to get to the planned departure month.
  28. WebElement defaultMonth = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='uitk-new-date-picker-desktop-months-container']//div[1]/h2")));
  29. System.out.println(defaultMonth.getText());
  30. while (true) {
  31. if (defaultMonth.getText().equals(DepartMonth)) {
  32. break;
  33. } else {
  34. wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='uitk-calendar']//button[2]"))).click();
  35. }
  36. }
  37. // Get the list of departure dates.
  38. List<WebElement> depDates = driver.findElements(By.xpath("//div[@class='uitk-calendar']//div[1]//table[1]//tbody[1]//td"));
  39. // Loop through the list and click on the intended departure date
  40. for (int i = 0; i < depDates.size(); i++) {
  41. if (depDates.get(i).getText().equals(DepartDate)) {
  42. depDates.get(i).click();
  43. }
  44. }
  45. // Click on the Done button.
  46. driver.findElement(By.cssSelector("button[data-stid='apply-date-picker']")).click();
  47. // Click on the return date field.
  48. driver.findElement(By.cssSelector("#d2-btn")).click();
  49. // Planned return month and date.
  50. String returnMonth = "November 2020";
  51. String returnDate = "22";
  52. // Navigate through the months to get to the desired return month.
  53. WebElement currentMonth = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='uitk-new-date-picker-desktop-months-container']//div[1]/h2")));
  54. while (true) {
  55. if (currentMonth.getText().equals(returnMonth)) {
  56. break;
  57. } else {
  58. driver.findElement(By.xpath("//div[@class='uitk-calendar']//button[2]")).click();
  59. Thread.sleep(1000);
  60. }
  61. }
  62. // List the return dates
  63. List<WebElement> rtnDates = driver.findElements(By.xpath("//div[@class='uitk-calendar']//div[1]//table[1]//tbody[1]//td"));
  64. // Now click on the planned return date date.
  65. for (int j = 0; j < rtnDates.size(); j++) {
  66. if (rtnDates.get(j).getText().equals(returnDate)) {
  67. rtnDates.get(j).click();
  68. }
  69. }
  70. // Click on the Done button.
  71. driver.findElement(By.cssSelector("button[data-stid='apply-date-picker']")).click();
  72. }
  73. }
英文:

I am trying to click on the arrow button until I reach to the planned departure month, which is November but it stops after one click. I used the same approach on airfare.com successfully but it's not working on expedia. It's throwing the error "StaleElementReferenceException: stale element reference: element is not attached to the page document"

Could anyone, please, help?

  1. package com.w2a.rough;
  2. import java.util.List;
  3. import java.util.concurrent.TimeUnit;
  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.chrome.ChromeDriver;
  8. import org.openqa.selenium.support.ui.ExpectedConditions;
  9. import org.openqa.selenium.support.ui.WebDriverWait;
  10. import io.github.bonigarcia.wdm.WebDriverManager;
  11. public class ExpediaDatePicker {
  12. public static void main(String[] args) throws InterruptedException {
  13. WebDriverManager.chromedriver().setup();
  14. WebDriver driver = new ChromeDriver();
  15. driver.manage().window().maximize();
  16. driver.manage().deleteAllCookies();
  17. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  18. WebDriverWait wait = new WebDriverWait(driver, 15);
  19. driver.get(&quot;https://www.expedia.com&quot;);
  20. //Click on the flight tab
  21. driver.findElement(By.cssSelector(&quot;#uitk-tabs-button-container &gt; li:nth-child(2) &gt; a&quot;)).click();
  22. //Click on Departing field to open the calendar
  23. driver.findElement(By.cssSelector(&quot;#d1-btn&quot;)).click();
  24. //Planned departure month and date
  25. String DepartMonth = &quot;November 2020&quot;;
  26. String DepartDate = &quot;15&quot;;
  27. //Navigate through the months to get to the planned departure month.
  28. WebElement defaultMonth = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[@class=&#39;uitk-new-date-picker-desktop-months-container&#39;]//div[1]/h2&quot;)));
  29. System.out.println(defaultMonth.getText());
  30. while(true) {
  31. if(defaultMonth.getText().equals(DepartMonth)) {
  32. break;
  33. }else {
  34. wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[@class=&#39;uitk-calendar&#39;]//button[2]&quot;))).click();
  35. }
  36. }
  37. //Get the list of departure dates.
  38. List&lt;WebElement&gt; depDates = driver.findElements(By.xpath(&quot;//div[@class=&#39;uitk-calendar&#39;]//div[1]//table[1]//tbody[1]//td&quot;));
  39. //Loop through the list and click on the intended departure date
  40. for(int i = 0; i &lt; depDates.size(); i++) {
  41. if(depDates.get(i).getText().equals(DepartDate)){
  42. depDates.get(i).click();
  43. }
  44. }
  45. //Click on the Done button.
  46. driver.findElement(By.cssSelector(&quot;button[data-stid=&#39;apply-date-picker&#39;]&quot;)).click();
  47. //Click on the return date field.
  48. driver.findElement(By.cssSelector(&quot;#d2-btn&quot;)).click();
  49. //Planned return month and date.
  50. String returnMonth = &quot;November 2020&quot;;
  51. String returnDate = &quot;22&quot;;
  52. //Navigate through the months to get to the desired return month.
  53. WebElement currentMonth = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[@class=&#39;uitk-new-date-picker-desktop-months-container&#39;]//div[1]/h2&quot;)));
  54. while(true) {
  55. if(currentMonth.getText().equals(returnMonth)) {
  56. break;
  57. }else {
  58. driver.findElement(By.xpath(&quot;//div[@class=&#39;uitk-calendar&#39;]//button[2]&quot;)).click();
  59. Thread.sleep(1000);
  60. }
  61. }
  62. //List the return dates
  63. List&lt;WebElement&gt; rtnDates = driver.findElements(By.xpath(&quot;//div[@class=&#39;uitk-calendar&#39;]//div[1]//table[1]//tbody[1]//td&quot;));
  64. //Now click on the planned return date date.
  65. for(int j = 0; j &lt; rtnDates.size(); j++) {
  66. if(rtnDates.get(j).getText().equals(returnDate)){
  67. rtnDates.get(j).click();
  68. }
  69. }
  70. //Click on the Done button.
  71. driver.findElement(By.cssSelector(&quot;button[data-stid=&#39;apply-date-picker&#39;]&quot;)).click();
  72. }
  73. }
  74. </details>

huangapple
  • 本文由 发表于 2020年7月25日 22:58:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/63089830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定