Selenium Webdriver-HTML DOM不变时,单击多个下拉列表时,陈旧元素异常 [英] Selenium Webdriver - Stale element exception when clicking on multiple dropdowns while HTML DOM doesn't change

查看:90
本文介绍了Selenium Webdriver-HTML DOM不变时,单击多个下拉列表时,陈旧元素异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图自动化一个方案,条件是我必须从下拉菜单中选择一个选项,然后旁边有另一个下拉菜单,我必须单击下一个下拉菜单中的一个选项才能启用button.我尝试使用该代码,但是它仅单击第一个选项.并且显示错误为陈旧的Element reference:element未附加到页面文档中.请帮忙.如果不清楚,请告诉我.

I tried to automate a scenario, where the condition is that I have to select an option from drop down and then there's another dropown next to it, I have to click one option from next drop to enable to button . I tried with the code but it clicks only the first option,.And showing error as stale Element reference:element is not attached to the page document. Please help. Please let me know if in not very clear.

推荐答案

当您选择那么只有你得到的选择,这实质上意味着HTML DOM得到改变,这导致.为避免这种情况,一旦我们从第一个下拉列表中进行选择,我们就需要为第二个下拉列表的元素引入一些wait,以呈现在HTML DOM中.然后,我们将使用Select类来选择一个选项.试用以下代码块:

When you select Insurance Test Client then only you get the option Product Insurance, which essentially means the HTML DOM gets changed, which results in StaleElementException. To avoid that, once we select from the first dropdown, we need to induce some wait for the elements of the second dropdown to render in the HTML DOM. Then we will use Select Class to select an option. Try out the following code block:

//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");

这篇关于Selenium Webdriver-HTML DOM不变时,单击多个下拉列表时,陈旧元素异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆