简体   繁体   English

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

[英]Selenium can't find iframe after postback inside the iframe after finding one element inside the frame

I am trying to create a test where I have to fill out some information inside an iframe.我正在尝试创建一个测试,我必须在 iframe 中填写一些信息。 Getting access to the iframe work fine and I can fill out information inside the frame.访问 iframe 工作正常,我可以在框架内填写信息。 The issue is that when I find out a element 'A' it has a postback attached to it which reloads the content inside the iframe, to find out another element 'B'.问题是,当我发现一个元素“A”时,它附加了一个回发,它会重新加载 iframe 内的内容,以找出另一个元素“B”。 So i am not able to find that element.I am getting below error:所以我找不到那个元素。我收到以下错误:

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)

Here are my observations: When I first locate the iframe it looks like this:以下是我的观察:当我第一次找到 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">

After the postback has occurred it looks like this:回发发生后,它看起来像这样:

<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">

I can easily find element A:我可以很容易地找到元素 A:

But element B i am not able to find The code fails when I try to get hold of the iframe element.但是我无法找到元素 B 当我尝试获取 iframe 元素时,代码失败。

How can I get hold of the iframe again, after the postback inside the frame?在框架内回发后,如何再次获得 iframe?

I have tried this suggestion also but it is not working我也尝试过这个建议,但它不起作用

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

Use a driver.executescript() for the first problem since another element is receiving the click.使用 driver.executescript() 解决第一个问题,因为另一个元素正在接收点击。

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

This error message...这个错误信息...

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)

...implies that the WebDriverException was raised as you tried to invoke click() on the <iframe> element. ...暗示当您尝试在<iframe>元素上调用click()时引发了WebDriverException

Factually, instead of clicking on the <iframe> element you would invoke click() on an element within the <iframe> .事实,而不是点击上<iframe>元素,你会调用click()中的一个元素<iframe> Moreover, as the the desired element is within a <iframe> so you have to:此外,由于所需元素位于<iframe>因此您必须:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt . Induce WebDriverWait以获得所需的frameToBeAvailableAndSwitchToIt
  • Induce WebDriverWait for the desired elementToBeClickable .为所需的elementToBeClickable引入WebDriverWait
  • You can use either of the following Locator Strategies :您可以使用以下任一定位器策略
    • Using xpath :使用xpath

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

References参考

You can find a couple of relevant discussions in:您可以在以下位置找到一些相关讨论:

I am able to extract the data of the active cases rise and fall for every state.我能够提取每个州的活跃案例上升和下降的数据。 There is a single frame which contains both the locators shared by you.有一个框架包含您共享的两个定位器。 Check below working code.检查以下工作代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM