【问题标题】:How to deal with parent and child iframes如何处理父子 iframe
【发布时间】:2018-08-02 16:45:42
【问题描述】:

我有一个场景,我的第一个 iframe(即父 iframe)上有一个按钮,点击它另一个 iframe 会打开(子 iframe)。我可以切换到父 iframe,但是当我单击按钮并尝试与子 iframe 交互时,我无法做到这一点。您能否建议让此类场景发挥作用的更好方法?

我的脚本:

public class Iframe {
    public static void main (String []args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://automation.cloudaccess.host/administrator"); 
        driver.findElement(By.id("mod-login-username")).sendKeys("admin");
        driver.findElement(By.id("mod-login-password")).sendKeys("admin@123");
        driver.findElement(By.id("mod-login-password")).submit();
        driver.findElement(By.linkText("Components")).click();
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.linkText("Messaging"))).build().perform();
        driver.findElement(By.linkText("New Private Message")).click();
        driver.findElement(By.className("wf-editor-header")).click();
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id=\"jform_message_imgmanager\"]"))).click();
        new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@src,'&plugin=imgmanager')]")));
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"browser-actions\"]/a[@id=\"help\"]"))).click();
        driver.switchTo().defaultContent();            
        new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@id=\"mce_inlinepopups_50_ifr\"]")));
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"imgmanager.insert\"]/i[@class=\"icon-file\"]"))).click();
        driver.quit();
    }
}

错误:

   Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for frame to be available: By.xpath: //iframe[@id="mce_inlinepopups_50_ifr"] (tried for 20 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
    at testScripts.Iframe.main(Iframe.java:51)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //iframe[@id="mce_inlinepopups_50_ifr"]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
System info: host: 'vowellt4', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-24-generic', java.version: '1.8.0_171'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.ExpectedConditions.lambda$findElement$0(ExpectedConditions.java:896)
    at java.util.Optional.orElseThrow(Optional.java:290)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:895)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44)
    at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:517)
    at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:513)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:248)
    ... 1 more

【问题讨论】:

  • 尝试从父框架切换到默认内容,然后切换到子框架
  • @theGuy:也尝试过这样做。但仍然遇到同样的错误。
  • 好的,那么请分享您的 html 代码
  • @theGuy:您可以在最后运行代码。这是我的测试站点,您可以登录并查看 html。
  • 如果下面的答案不起作用,那么您可以将网站和凭据发送给我,我可以查看。

标签: selenium iframe


【解决方案1】:

单击帮助按钮后,您可以尝试使用此代码:

 driver.switchTo().defaultContent();
 new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src^='/administrator/index.php?option=com_jce&view=help&tmpl=component&lang=en&section=editor&category=imgmanager&']")));  

您正在使用//iframe[@id=\"mce_inlinepopups_50_ifr\"]这个xpath切换到框架,但问题是id是动态生成的,所以我们不知道每次我们通过自动化访问页面时id是什么。

我只是简单地将该 xpath 转换为有效且可靠的 css 选择器,它在我的最后工作得非常好。

如果你想拥有 xpath :

//iframe[contains(@src,'/administrator/index.php?option=com_jce&view=help&tmpl=component&lang=en&section=editor&category=imgmanager&')]

希望这会有所帮助。

【讨论】:

  • 它对我有用!谢谢一堆。 CSS 选择器对我有用.. xpath 没有。不知道为什么,因为我能够通过两个选择器找到元素,但在执行脚本时 CSS 选择器赢了。
  • 理想情况下 xpath 也应该可以工作,但 css 选择器更可靠、一致且强大。你可以坚持使用 CSS 选择器。
  • 感谢您的帮助!但除此之外,在返回时,即当我们从子 iframe 切换回父 iframe 时,我们是否必须使用 switchTo.defaultcontent(); ?或不需要。
  • 您应该切换到默认内容,以避免不一致。所以是的,这是必需的。
【解决方案2】:

点击帮助按钮后,需要从父 iframe 切换到默认内容,然后需要导航到相应的子框架。

Child Frame Xpath 需要更改如下

Xpath: //*[@class='mceModalContainer']//div[@id='mce_inlinepopups_45_content']//iframe

修改后的工作代码:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"browser-actions\"]/a[@id=\"help\"]"))).click();

driver.switchTo().defaultContent();

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//*[@class='mceModalContainer']//div[@id='mce_inlinepopups_45_content']//iframe")));

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='help-menu']//*[@id=\"imgmanager.insert\"]/i[@class=\"icon-file\"]"))).click();

【讨论】:

    猜你喜欢
    • 2011-11-26
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多