无法使用Selenium Webdriver切换到iframe

4
我正在尝试点击位于class属性为"modalIframe"的iframe内部的SIGN IN链接。我已经尝试了过去两天寻找解决方案,但一直没有成功。非常感谢您的帮助。
以下是代码:
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();


    }
}

运行代码时出现以下错误:

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


你能分享完整的错误日志吗? - Giri
2个回答

4

使用以下代码处理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.

0
在探索HTML并尝试不同的XPATH以查找元素时,观察到有相同的元素属性存在 3个元素。因此,要使其唯一,请从 GooglePlus注册元素开始构建相对XPATH,然后找到相对于该元素的登录链接。
请尝试以下代码:
    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.

建议不要仅提供代码作为答案。您应该解释原始问题的代码为什么不能正常运行以及您的代码如何解决这个问题。此外,是否有没有使用Thread.sleep()的方法?除非绝对必要,否则一般认为这种做法很糟糕。 - ExoticChimp
@ExoticChimp,我编辑了答案以解释我所做的事情来解决问题。感谢您的好建议。 - Naveen Kumar R B
感谢@Naveen。在删除Thread.sleep()的情况下,使用WebDriverWait代码也可以正常工作。另外,关于SIGN IN元素,我也尝试过使用linkText,但也没有成功。 - Madan
很高兴它对你有用。是的,linkText可能无法返回正确的元素,因为有3个具有相同文本的元素。因此,从GooglePlusSignin派生出XPATH,可以精确地找到所需的元素。 - Naveen Kumar R B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接