在帧内找到一个元素后,Selenium 在 iframe 内回发后找不到 iframe



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

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

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

此错误消息。。。

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是在您尝试调用<iframe>元素上的click()时引发的。

实际上,您将在<iframe>中的一个元素上调用click(),而不是单击<iframe>元素。此外,由于所需元素在<iframe>中,因此您必须:

  • 诱导WebDriverWait以获得所需的帧ToBeAvailableAndSwitchToIt
  • 诱导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下的#文档
  • 在SeleniumWebdriverJava中,是否可以在不使用driver.switchTo((.frame("frameName"(的情况下切换到框架中的元素

我能够提取每个状态的活跃病例上升和下降的数据。有一个单独的框架,其中包含您共享的两个定位器。检查以下工作代码。

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

最新更新