【问题标题】:Selenium Webdriver - Stale element exception when clicking on multiple dropdowns while HTML DOM doesn't changeSelenium Webdriver - 在 HTML DOM 不变时单击多个下拉菜单时出现陈旧元素异常
【发布时间】:2017-12-16 19:20:38
【问题描述】:

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

【问题讨论】:

  • 给我们任何代码。你如何选择选项?另外,我不明白您对按钮及其旁边的列表的看法。什么启用什么?
  • 当您选择Self 时,只有您获得General 选项,这实质上意味着HTML DOM 被更改,从而导致StaleElementException
  • 代码在哪里?
  • 我猜,在选择第一个下拉列表之前,您会找到第二个下拉列表的元素。您必须执行以下操作,首先选择第一个下拉列表,然后在第二个下拉列表中找到要选择的元素并单击
  • //选择频道 Select oSelectChannel = new Select(driver.findElement(By.id("client"))); oSelectChannel.selectByVisibleText("保险测试客户端"); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 'code' //选择类别选择 oSelectCategory = new Select(driver.findElement(By.xpath("//*[@id='category']"))); oSelectCategory.selectByVisibleText("产品保险");

标签: java selenium selenium-webdriver webdriverwait staleelementreferenceexception


【解决方案1】:

当您选择Insurance Test Client 时,只有您获得Product Insurance 选项,这实质上意味着HTML DOM 被更改,从而导致StaleElementException。为了避免这种情况,一旦我们从第一个下拉列表中进行选择,我们需要为第二个下拉列表的元素引入一些wait 以在HTML DOM 中呈现。然后我们将使用Select Class 来选择一个选项。试试下面的代码块:

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

【讨论】:

  • 感谢@DebanjanB。有效。我不想使用等到条件。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2012-09-05
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
相关资源
最近更新 更多