【问题标题】:Selenium can't find iframe after postback inside the iframe after finding one element inside the frame在框架内找到一个元素后,Selenium 在 iframe 内回发后找不到 iframe
【发布时间】:2020-12-29 16:33:43
【问题描述】:

我正在尝试创建一个测试,我必须在 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>')]")));

【问题讨论】:

  • 如果您可以提及使用目标网址重现此内容的步骤。
  • Element ... is not clickable at point (554, 7) 表示您可以找到元素,但您无法单击它,因为另一个元素与它重叠。它可以是弹出窗口,或者您需要滚动到您的元素。也可以尝试使用js点击。
  • 我仍然没有看到您在问题中提到的回发。我错过了什么吗?
  • @DilipMeghwal 这是我正在尝试的网址:ndtv.com/coronavirus/india-covid-19-tracker----In 这是我正在尝试自动化我需要采取状态明智的活动案例。因此,对于状态明智的活动案例,有两个 xpath: private By TotalTodayActiveCasesforStatesDown=By.xpath("//tr//td[3]//p//span[@class='data-down']"); private By TotalTodayActiveCasesforStatesUp=By.xpath("//tr//td[3]//p//span[@class='data-up']");
  • @AliaksandrPlekau:我尝试使用 javascript 也无法正常工作

标签: java selenium xpath iframe css-selectors


【解决方案1】:

对第一个问题使用 driver.executescript(),因为另一个元素正在接收点击。

element = driver.findElement(By.id(""));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);

【讨论】:

    【解决方案2】:

    此错误消息...

    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)
    

    ...暗示当您尝试在 &lt;iframe&gt; 元素上调用 click() 时引发了 WebDriverException

    实际上,您可以在&lt;iframe&gt; 中的一个元素上调用click(),而不是单击&lt;iframe&gt; 元素。此外,由于所需元素位于 &lt;iframe&gt; 内,因此您必须:

    • 为所需的 frameToBeAvailableAndSwitchToIt 诱导 WebDriverWait
    • 为所需的 elementToBeClickable 诱导 WebDriverWait
    • 您可以使用以下任一Locator Strategies
      • 使用xpath

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

    参考文献

    您可以在以下位置找到一些相关讨论:

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-30
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        • 2021-06-12
        • 2021-10-13
        • 2019-07-01
        • 1970-01-01
        相关资源
        最近更新 更多