无法使用Selenium webdriver切换到iframe [英] Not able to switch to iframe using Selenium webdriver

查看:488
本文介绍了无法使用Selenium webdriver切换到iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图点击SIGN IN链接,该链接位于iframe中,类属性为modalIframe。在过去的两天里,我一直在努力寻找解决方案,但却无法做到。任何帮助都将非常感激。

I am trying to click on SIGN IN link which is placed inside the iframe with class attribute "modalIframe". I have been trying for the past two days to find a solution for this but not able to. Any help would be really appreciated.

代码如下

public class Datereader
{

    public static void main(String[] args)
    {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\Madankumar\\Desktop\\Gecko Driver\\geckodriver.exe");
        WebDriver driver=new FirefoxDriver();
        driver.get("https://www.redbus.in/");
        driver.manage().window().maximize();
        driver.findElement(By.xpath(".//div[@class='icon-down icon ich dib']")).click();
        driver.findElement(By.id("signInLink")).click();    
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        WebElement iframeElement=driver.findElement(By.xpath("//*[@class='modalIframe']"));
        driver.switchTo().frame(iframeElement);
        driver.findElement(By.linkText("sign in")).click();


    }
}

运行时代码我收到以下错误:

On running the code I am getting below error:


JavaScript警告: https://cdn-jp.gsecondscreen.com/static/tac.min.js ,第3行:返回语句后无法访问的代码

JavaScript warning: https://cdn-jp.gsecondscreen.com/static/tac.min.js, line 3: unreachable code after return statement


推荐答案

使用以下代码处理iframe。

WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);           //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
driver.findElement(By.linkText("sign in")).click();
driver.switchTo().defaultContent();       //Move outside to the frame.

这篇关于无法使用Selenium webdriver切换到iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆