【问题标题】:Selenium WebDriver - Getting NoSuchElementException error even element is presentSelenium WebDriver - 即使元素存在,也会出现 NoSuchElementException 错误
【发布时间】:2015-01-14 16:19:30
【问题描述】:

我正在尝试单击应用程序中的下拉框,而 Xpath 为://*[@id='sel2O5_chzn']/a 但是当我尝试点击它时,我得到 NoSuchElementException 偶数元素存在于应用程序中。在 IE 中试过,Chrome 和 Firefox 仍然面临同样的问题

driver.findElement(By.xpath("//*[@id='sel2T1_chzn']/a")).click();

也尝试过以下方法,但没有运气。得到相同的 NoSuchElementException

Select sel = new Select(driver.findElement(By.xpath("//*[@id='sel2T1_chzn']")));
sel.selectByIndex(1);

请帮我解决这个问题

【问题讨论】:

  • 能否分享下拉菜单的相关HTML代码?
  • 可能有两种可能性。 1 - 元素需要时间来显示。 2 - 元素在 iframe 或框架中。你能探索这两种可能性吗?如果您可以提供 HTML sn-p,它将帮助我们帮助您。

标签: java selenium webdriver


【解决方案1】:

使用 explicit 等待,它使您能够等待由 WebDriver 定位的元素,here 是您可以使用的 ExpectedConditions 列表

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='sel2T1_chzn']")));
myDynamicElement.click();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2018-10-18
    • 2021-05-26
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多