我得到了这个异常。请解析it.org.openqa.selenium.StaleElementReferenceException:在缓存中找不到元素 - 也许页面自查找以来已更改 命令持续时间或超时:5.10秒
CODE:
List<WebElement> select_year = driver.findElements(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele: select_year)
{
String fyear=ele.getText();
if((syear).equals(fyear))
{
System.out.println(syear);
System.out.println(fyear);
ele.click();
List<WebElement>select_month=driver.findElements(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele2:select_month)
{
String fmonth=ele2.getText();
if((smonth).equals(fmonth))
{
ele2.click();
List<WebElement>select_day=driver.findElements(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele3:select_day)
{
String fday=ele3.getText();
Thread.sleep(3000);
if((sday).equals(fday))
{
ele3.click();
}
}
}
}
}
}
答案 0 :(得分:1)
无需在这里循环。您需要找到包含文本的单个元素,然后选择如下: -
//To select year
driver.findElement(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span[text() = " + syear+"]")).click();
//To select month
driver.findElement(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span[text() = " + smonth+"]")).click();
//To select day
driver.findElement(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span[text() = " + sday+"]")).click();
已修改: - 如果您想在此处循环,则只需break
条件if
条件变为真,如下所示: -
//To select year
List<WebElement> select_year = driver.findElements(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele: select_year)
{
String fyear=ele.getText();
if((syear).equals(fyear))
{
ele.click();
break;
}
}
//To select month
List<WebElement> select_month = driver.findElements(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele: select_year)
{
String fmonth = ele.getText();
if((smonth).equals(fmonth))
{
ele.click();
break;
}
}
//To select day
List<WebElement> select_day = driver.findElements(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele: select_year)
{
String fday = ele.getText();
if((sday).equals(fday))
{
ele.click();
break;
}
}