首页 > 解决方案 > 切换到 iframe 时无法定位元素?

问题描述

  1. 登录后尝试通过奖励帐户通过 Facebook 登录我让 iframe 将用户与奖励帐户连接,我正在使用窗口处理程序来定位从该框架弹出的 facebook,然后它在登录后关闭,我返回 iframe 但无法找到元素在父窗口(iframe)上,当我切换回来时,我没有得到这样的元素
  2. 错误是:

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//*[@id='errorData']"}

  1. 使用的代码

    public void amberLoginWithFacebook(String username, String pass) throws InterruptedException {
    
    actions.driver.switchTo().defaultContent();
    actions.driver.switchTo().frame(0);
    actions.clickOn(By.cssSelector(facebookBtn));
    Thread.sleep(2000);
    Set<String> handler = actions.driver.getWindowHandles();
    Iterator<String> loop = handler.iterator();
    String parentWindow = loop.next();
    String childWindow = loop.next();
    actions.driver.switchTo().window(childWindow);
    if (actions.driver.getTitle().toLowerCase().equals(facebookWindowTitle)) {
    actions.setText(By.cssSelector(facebookEmail), username);
    actions.setText(By.cssSelector(facebookPass), pass);
    actions.clickOn(By.cssSelector(facebookLoginBtn));
    Thread.sleep(2000);
    
    }
    
    actions.driver.switchTo().window(parentWindow);
    // i get the exception here 
    if (actions.waitUntil(By.xpath("NonlinkedAccountError"), "presenceOfElement") != null)
    {setCountryCode(properties.getProperty("CountryCode"));
    setPhone(properties.getProperty("phone"));
    actions.clickOn(By.xpath(defaultBtn));
    actions.clickOn(By.xpath(enterEmail));
    setEmail(properties.getProperty("email"));
    }
    

框架的屏幕截图

标签: javaseleniumiframeframes

解决方案


推荐阅读