首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium Webdriver :我无法切换到iframe

Selenium Webdriver :我无法切换到iframe
EN

Stack Overflow用户
提问于 2017-07-19 10:47:41
回答 2查看 3.6K关注 0票数 0

下面是我写的代码,我尝试过添加thread.sleep(),但是它仍然不能工作,也尝试过使用chromedriver,但是结果是一样的

代码语言:javascript
复制
package com.thinksys.frames;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Iframes 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\thinksysuser\\Downloads\\geckodriver-v0.18.0-win64\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.irctc.co.in/eticketing/loginHome.jsf");

        WebElement e = driver.findElement(By.id("google_ads_iframe_/37179215/DFP_NGET_01_HomePage_RHS_ATF_479x266_ENG_0"));

        driver.switchTo().frame(e);

        driver.findElement(By.xpath(".//*[@id='image-11']/a/img")).click();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-19 11:08:31

它可能是由<ifram> id中的特殊字符引起的。使用部分id将提供两个匹配,因此我建议您使用name属性的两个部分。

代码语言:javascript
复制
WebElement frame = driver.findElement(By.cssSelector("[name*='google_ads_iframe'][name*='DFP_NGET_01_HomePage_RHS']"));
driver.switchTo().frame(frame);

编辑

图像旋转,每个打开只显示几秒钟。要单击特定的图像,您需要等待它是可见的。您可以使用显式等待。

代码语言:javascript
复制
WebDriverWait wait = new WebDriverWait(driver, 60, 50);
wait.until(ExpectedConditions.visibilityOfElementLocatedBy.xpath(".//*[@id='image-11']/a/img"))).click();

这将每100毫秒使DOM极化,直到图像可见或时间延长(60秒)。

票数 1
EN

Stack Overflow用户

发布于 2017-07-19 11:59:26

有不同的浏览器来显示广告。我在Firefox中打开过,当它在chrome上显示时,我无法看到它。而且,当我使用脚本打开Chrome或Firefox浏览器时,没有广告部分。

根据这个场景,您可以检查天气框架是否可用,如果可用,然后切换到它,直到您要单击的图像变得可见,然后单击它。

你可以试试这个方法:

代码语言:javascript
复制
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(driver.findElement(By.xpath("//iframe[starts-with(@id,'google_ads_iframe')][@title='3rd party ad content']"))));

wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[@id='image-11']/a/img")))).click();

代码语言:javascript
复制
WebDriverWait wait = new WebDriverWait(driver, 120);
List <WebElement> adFrame = driver.findElements(By.xpath("//iframe[starts-with(@id,'google_ads_iframe')][@title='3rd party ad content']"))
   if(adFrame.size()>0)
    {
        driver.switchTo().frame(adFrame.get(0));
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[@id='image-11']/a/img")))).click();
    }
    else
    {
        System.out.println("Sorry there is no ads");
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45188293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档