【问题标题】:Not able to switch to iframe using Selenium webdriver无法使用 Selenium webdriver 切换到 iframe
【发布时间】:2017-05-06 09:39:42
【问题描述】:

我正在尝试单击位于具有类属性“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 语句后无法访问代码

【问题讨论】:

  • 你能分享完整的错误日志吗

标签: java selenium iframe automated-tests


【解决方案1】:

在探索 HTML 并尝试不同的 XPATH 来查找元素时,观察到存在具有相同元素属性的 3 elements。因此,为了使其独一无二,从 GooglePlus Signup 元素开始构建相对 XPATH,然后找到与该元素相关的 sign in 链接。

试试下面的代码:

    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.

【讨论】:

  • 不鼓励仅使用代码的答案。您应该解释 OPs 代码不起作用的原因以及您的代码如何解决这个问题。还有没有办法避免Thread.sleep()?除非绝对必要,否则这通常被认为是非常糟糕的做法
  • @ExoticChimp,编辑了答案以解释我为解决问题所做的事情。谢谢你的好建议。
  • 感谢@Naveen。该代码在删除 Thread.sleep() 时与 WebDriverWait 一起工作。此外,关于 SIGN IN 元素,我也尝试过使用 linkText,但这甚至没有成功。
  • 很高兴它对你有用。是的,linkText 可能不会返回正确的元素,因为有 3 个这样的元素具有相同的文本。因此从 GooglePlusSignin 派生了 XPATH,它准确地找到了所需的元素。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-24
    • 2023-03-08
    • 2018-12-31
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    相关资源
    最近更新 更多