当前位置: 首页 > 知识库问答 >
问题:

Selenium在框架内找到一个元素后在iframe内回发后找不到iframe

黎同
2023-03-14

我正在尝试创建一个测试,在这个测试中,我必须在iframe中填写一些信息。访问iframe工作正常,我可以在框架内填写信息。问题是,当我找到一个元素“a”时,它会附加一个回发,它会在iframe中重新加载内容,以找到另一个元素“B”。所以我找不到那个元素。我遇到以下错误:

org.openqa.selenium.WebDriverException: unknown error: Element <iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&amp;days=7" cd_frame_id_="7da8f2aea5a580b3a6e90a9d5016fa0d">...</iframe> is not clickable at point (554, 7). Other element would receive the click: <div class="topnav2014" style="border-bottom: none;">...</div>
  (Session info: chrome=85.0.4183.83)

以下是我的观察:当我第一次找到iframe时,它看起来像这样:

<iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&amp;days=7">

发生回发后,如下所示:

<iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&amp;days=7" cd_frame_id_="a5006acf28d8c288313681ab9ad7a4fa">

我可以很容易地找到元素A:

但是元素B我无法找到代码,当我尝试获取iframe元素时,代码失败了。

在帧内回发后,我如何才能再次获得iframe?

我也尝试过这个建议,但不起作用

//Ensure that you are back to the base frame  
driver.SwitchTo().DefaultContent();
//SwitchTo the intended frame
 driver.SwitchTo().Frame(driver.FindElement(By.XPath("//iframe[contains(@src,'<removed for clearity>')]")));

共有3个答案

巫马自明
2023-03-14

我能够提取每个状态的活动案例上升和下降的数据。有一个框架包含您共享的两个定位器。检查下面的工作代码。

    driver.get("https://www.ndtv.com/coronavirus");
    driver.manage().window().maximize();
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(By.className("tab-wrapper")));
    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1')]")));
    
    //cases down
    List<WebElement> eleCasesUp = driver.findElements(By.xpath("//tr//td[3]//p//span[@class='data-up']"));
    List<String> casesUpList = new ArrayList<String>();
    for (WebElement element : eleCasesUp) { 
        casesUpList.add(element.getText()); 
    }
    
    //cases up
    List<WebElement> eleCasesDown = driver.findElements(By.xpath("//tr//td[3]//p//span[@class='data-down']"));
    List<String> casesDownList = new ArrayList<String>();
    for (WebElement element : eleCasesDown) { 
        casesDownList.add(element.getText()); 
    }
    
    System.out.println("Cases Up List -->" + casesUpList);
    System.out.println("Cases Down List -->" + casesDownList);
谷梁宏恺
2023-03-14

此错误消息。。。

org.openqa.selenium.WebDriverException: unknown error: Element <iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&amp;days=7" cd_frame_id_="7da8f2aea5a580b3a6e90a9d5016fa0d">...</iframe> is not clickable at point (554, 7). Other element would receive the click: <div class="topnav2014" style="border-bottom: none;">...</div>
  (Session info: chrome=85.0.4183.83)

...意味着WebDriverException是在您尝试调用上的

事实上,不是点击

>

  • 为所需的FrameToBeavailable和SwitchToIt诱导WebDriverWait
  • 将所需元素的WebDriverWait诱导为可访问
  • 您可以使用以下定位器策略之一:
    • 使用xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe_xpath")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
      

      您可以在以下内容中找到一些相关讨论:

      • iframe下#文档的处理方法

  • 濮阳旺
    2023-03-14

    使用驱动程序。executescript()用于解决另一个元素收到单击后的第一个问题。

    element = driver.findElement(By.id(""));
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].click();", element);
    
     类似资料:
    • 我想运行一个selenium测试,当单击旧页面中的按钮时,它是否导航到新页面。然后我想测试当新页面中的按钮被点击时,它是否导航回旧页面。 当我单击第一页上的按钮并检查新页面上的按钮元素是否出现时,测试工作正常。但是当我使用selenium点击第二页上的button元素,检查第一页上的button元素是否已经出现时,它没有找到第一页上的button元素,尽管我第一次使用相同的button元素去第二页

    • 我面临的错误截图 上面的一行显示了错误,它表示。我在堆栈溢出上看到过这种问题,但解决方案对我没有帮助

    • 问题内容: 我将Firebase库添加到我的项目中,然后出现此错误。当我编译它时,Xcode找不到一些目录。但是,它们位于Pods目录中。 这是错误日志: 这是我的Podfile: 问题答案: 确保打开的是工作空间文件而不是项目文件。我收到相同的错误,并意识到我正在使用项目而不是工作区。

    • 问题内容: 我有以下XPath: 当我在XPath Checker(Firefox扩展)中试用此XPath时,它一直都能完美运行。但是当我在Selenium中执行以下操作时: 它不断给我以下日志错误: 我为解决这个问题而疯狂。有人看到我的代码行有任何错误吗? 问题答案: 该查询字符串不应该像这样(根据javadoc api)吗?

    • 我尝试了xpath和检查,如下所示

    • 我正在尝试以下元素: 以下是Java代码: 它找不到元素。请帮忙。谢谢。