【问题标题】:Selenium Cant find element inside Iframe event tho they are visibleSelenium无法在Iframe内找到元素,即使它们是可见的
【发布时间】:2018-08-26 12:31:59
【问题描述】:

我一直在尝试创建代码来自动登录我玩的名为 Life is Feudal 的游戏的网站以收集一些信息(一切都是合法的,我可以手动访问我想要的信息,但需要数年时间才能收集一切)。

这是我的代码:

    public static void main(String[] args) throws InterruptedException {

    String Url = "https://lifeisfeudal.com/";
    String email = "MYEMAIL";
    String password = "MYPASSWORD";

    WebDriver driver = setDriver();

    // Open Life is feudal home page
    driver.get(Url);

    /*
     * Logging into my account
     */

    // Click on sign in button [WORK]
    driver.findElement(By.linkText("Sign In")).click();

    //Focus the Iframe
    driver.switchTo().frame(driver.findElement(By.id("signin")));

    /*
     * Type in Email
     */

    //Try #1 using name (DONT WORK)
    driver.findElement(By.name("email")).sendKeys(email);
    //Try #2 using xpath (DONT WORK)
    driver.findElement(By.xpath("//*[@id=\"react-view\"]/div/div/div/div[2]/form/div[1]/div/input")).sendKeys(email);

    /*
     * Type in Password
     */

    //Try #1 using name (DONT WORK)
    driver.findElement(By.name("password")).sendKeys(password);

    //Try #2 using xpath (DONT WORK)
    driver.findElement(By.xpath("//*[@id=\"react-view\"]/div/div/div/div[2]/form/div[2]/div/input")).sendKeys(password);

    /*
     * Click the sign in button
     */

    //Try (Havent tried it yet)
    driver.findElement(By.linkText("Sign in")).click();


    //Bring back to default
    driver.switchTo().defaultContent();
    //Rest of the code
}

这是 Iframe 中的 html 代码 HTML Iframe

编辑 1:按照 Frank 的建议在切换框架后添加一个 thread.sleep(3000) 使其工作。

我认为它没有完全加载框架并且试图访问当时不存在的未完全加载的东西。

【问题讨论】:

    标签: java html selenium iframe


    【解决方案1】:

    在切换到 IFrame 后添加一个 Thread.Sleep(3000) 以确定是否是时间问题。如果是这样,请将 Thread.Sleep(3000) 替换为更强大的等待方式,例如显式等待。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 2023-03-07
      • 2018-06-04
      • 2023-01-02
      • 1970-01-01
      • 2018-11-16
      相关资源
      最近更新 更多