Not able to switch to iframe using Selenium webdri

2019-07-14 07:15发布

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.

Code as below

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 warning: https://cdn-jp.gsecondscreen.com/static/tac.min.js, line 3: unreachable code after return statement

2条回答
Rolldiameter
2楼-- · 2019-07-14 07:36

Use Below code for handling 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.
查看更多
成全新的幸福
3楼-- · 2019-07-14 07:37

While exploring the HTML and trying out different XPATHs to find the element, it is observed that there are 3 elements present with the same element attributes. so, to made it unique, build the relative XPATH starting from GooglePlus Signup element, then find the sign in link relative to that.

try the following code:

    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);
    WebElement elem = driver.findElement(By.xpath("//div[@id='googlePlusBtn1']/following-sibling::div/span/a"));
    System.out.println("element " + elem);
    Thread.sleep(1000); // without this line, observed that click is not resulting in displaying the Login form, though selenium did not throw any error (means, click did not result in Login form). you can alternatively try out with WebDriverWait. this is trial and error. if it is working for you without sleep, you can remove this line.
    elem.click();
    Thread.sleep(5000); // can remove sleep. kept only for visual confirmaiton to check whether Login form is displayed, as there are no steps further after this in the code.
查看更多
登录 后发表回答