gpt4 book ai didi

java - Selenium Webdriver - 单击多个下拉菜单时出现陈旧元素异常,而 HTML DOM 不会更改

转载 作者:行者123 更新时间:2023-11-30 06:36:59 26 4
gpt4 key购买 nike

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

enter image description here

最佳答案

当您选择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");

关于java - Selenium Webdriver - 单击多个下拉菜单时出现陈旧元素异常,而 HTML DOM 不会更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45056398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com