Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium webdriver:stale元素引用:元素未附加到页面文档_Java_Selenium Webdriver - Fatal编程技术网

Java Selenium webdriver:stale元素引用:元素未附加到页面文档

Java Selenium webdriver:stale元素引用:元素未附加到页面文档,java,selenium-webdriver,Java,Selenium Webdriver,我有一个下拉列表,第一次可以通过索引选择元素。当我第二次尝试选择元素时,它抛出了陈旧的元素引用错误。我试着用try-catch-block,显式等待,但没有效果 WebElement drop = driver.findElement(By.cssSelector("#ctl00_mainPanel_MainPanel1_SearchStop1_DropDownRoute")); Select sel_drop = new Select(drop); List<WebElement>

我有一个下拉列表,第一次可以通过索引选择元素。当我第二次尝试选择元素时,它抛出了陈旧的元素引用错误。我试着用try-catch-block,显式等待,但没有效果

WebElement drop = driver.findElement(By.cssSelector("#ctl00_mainPanel_MainPanel1_SearchStop1_DropDownRoute"));

Select sel_drop = new Select(drop);
List<WebElement> drop_count = sel_drop.getOptions();
int drop_size = drop_count.size();
System.out.println("size of drop down" + drop_size);
sel_drop.selectByIndex(1);


JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(250,0)", "");

sel_drop.selectByIndex(10);//this line is causing error-- when I am trying to select element from dropbox for second time.
当您以前找到的元素不再附加到DOM HTML源时,会发生StaleElementReferenceException。它已被更改,需要重新查找。元素已更改,因为您执行了选择操作,并且其值已更改

解决方案:按如下方式再次查找元素:

WebElement drop = driver.findElement(By.cssSelector("#ctl00_mainPanel_MainPanel1_SearchStop1_DropDownRoute"));

Select sel_drop = new Select(drop);
List<WebElement> drop_count = sel_drop.getOptions();
int drop_size = drop_count.size();
System.out.println("size of drop down" + drop_size);
sel_drop.selectByIndex(1);

//below lines are crucial
drop = driver.findElement(By.cssSelector("#ctl00_mainPanel_MainPanel1_SearchStop1_DropDownRoute"));
sel_drop = new Select(drop);

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(250,0)", "");

sel_drop.selectByIndex(10);//this line is causing error-- when I am trying to select element from dropbox for second time.

在缺少相关HTML的情况下,很难猜测您在尝试选择第二个选项时看到的原因,但在这一点上值得一提的是,一个有效的用例将包含根据HTML DOM仅选择单个元素的步骤,并继续执行其他步骤。所以,根据您的代码块,如果第一个selectByIndex的选择有效,就可以开始了

WebElement drop = driver.findElement(By.cssSelector("#ctl00_mainPanel_MainPanel1_SearchStop1_DropDownRoute"));
Select sel_drop = new Select(drop);
List<WebElement> drop_count = sel_drop.getOptions();
int drop_size = drop_count.size();
System.out.println("size of drop down" + drop_size);
sel_drop.selectByIndex(1);
为什么连续选择不起作用 如果选择第一个调用JavaScript或AjaxCall,则可能会更改该调用,页面上可能会出现新的相关元素,在这种情况下,标记可能会更改页面中的位置。因此,当您尝试选择第二个标记时,需要再次识别标记,然后尝试选择第二个标记


在这里,您可以找到关于

的详细讨论,您声明第二次尝试失败,但代码仅显示一次尝试。两次尝试之间会发生什么?选择选项的行为可能会改变DOM,甚至可能改变select元素本身,从而使元素过时。在第二次尝试之前,我会尝试重新初始化select,因为选项可能已更改。sel\u drop.selectByIndex10;是第二次尝试。您是否已确认select中存在索引为10的选项?选择索引1后,select是否重新填充?是,它存在。问题解决了。谢谢,德班詹。我一定会检查链接的。在我的测试场景中,有两个下拉列表,我正在尝试选择这两个下拉列表的第一个、最后一个和中间索引组合,并希望确保结果是正确的。谢谢,Rafal。你的这段代码运行得很好。在过去的几天里,我一直在努力寻找解决办法。这真的很有帮助。