我被困在下面:
List<WebElement>lstType=driver.findElements(By.xpath("//select[@id='ddlGeneralType']/option"));
Thread.sleep(2000);
int i,j;
System.out.println("the no of type"+lstType.size());
for(i=1;i<=lstType.size();i++){
lstType.get(i).click();
List<WebElement> slctsubType=driver.findElements(By.xpath("//select[@id='ddlGeneralSubType']/option"));
System.out.println("the no of type"+slctsubType.size());
for(j=1;j<=slctsubType.size();j++){
try {
slctsubType.get(j).click();
Thread.sleep(5000);
WebElement save= driver.findElement(By.xpath("//div[@id='generalClassificationEditModal']/div[@class='modal-footer']/a[text()='Save']"));
save.click();
Thread.sleep(5000);
if(save.getText()==null){
// driver.findElement(By.xpath("//div[@id='generalClassificationModal']/div[@class='modal-footer']/button[text()='Close']")).click();
driver.findElement(By.xpath("//a[@href='#classification'and contains(@onclick,'OpenEditGeneralClassification();')]")).click();
}
else {
Thread.sleep(5000);
//driver.findElement(By.xpath(("//div[@id='generalClassificationEditModal']/div[@class='modal-footer']/button[text()='Close']"))).click();
driver.findElement(By.xpath(("//div[@id='generalClassificationEditModal']/div[@class='modal-footer']/button[text()='Cancel']"))).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//a[@href='#classification'and contains(@onclick,'OpenEditGeneralClassification();')]")).click();
}
}
catch(org.openqa.selenium.StaleElementReferenceException e){
e.printStackTrace();
}
}
}我收到一条由这一行代码引发的错误消息:
slctsubType.get(j).click(); 发布于 2015-06-12 06:57:42
造成这种情况的原因有两个:
在您的例子中,我认为因为元素j在slcsubType中不可见,所以它不能单击slctsubType.get(j).click(); --我认为您应该使用Explicit Wait,等待这些元素出现,然后单击。
希望这能有所帮助。
发布于 2015-06-12 06:40:14
我认为您已经编写了从下拉列表中选择所有选项的代码。但您最多应该选择一个元素。这样你就可以得到“硒中的StaleElementReferenceException”。
这意味着对现有元素的引用被取消引用,这意味着element=null。
问题在for循环中:
for(j=1;j<=slctsubType.size();j++){
try{
slctsubType.get(j).click();在上面的代码中,您将从下拉列表中选择所有选项。在任何条件下,如值或文本,只能选择一个选项。
https://sqa.stackexchange.com/questions/13447
复制相似问题