Selenium Webdriver - 在HTML DOM没有更改时单击多个下拉列表时出现过时的元素异常

时间:2017-07-12 11:27:10

标签: java selenium selenium-webdriver webdriverwait staleelementreferenceexception

我尝试自动化一个场景,其中条件是我必须从下拉列表中选择一个选项,然后在它旁边再显示一个选项,我必须从下一个点击中单击一个选项以启用按钮。我尝试使用代码,但它只点击第一个选项,。并将错误显示为陈旧元素引用:元素未附加到页面文档。请帮忙。如果不是很清楚,请告诉我。

enter image description here

1 个答案:

答案 0 :(得分:1)

当您选择Insurance Test Client时,只会获得选项Product Insurance,这实际上意味着HTML DOM会发生变化,从而产生StaleElementException。为避免这种情况,一旦我们从第一个下拉列表中进行选择,我们需要为wait中的第二个下拉列表的元素引入一些HTML DOM。然后我们将使用Select类来选择一个选项。试试下面的代码块:

//Select Channel 
Select oSelectChannel = new Select(driver.findElement(By.id("client"))); 
oSelectChannel.selectByVisibleText("Insurance Test Client"); 

WebDriverWait wait5 = new WebDriverWait(driver, 10);
wait5.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_a_Category_item")));

//Select Category 
Select oSelectCategory = new Select(driver.findElement(By.xpath("//*[@id='category']"))); 
oSelectCategory.selectByVisibleText("Product Insurance");