无法切换到框架,即使我通过了正确的框架ID或xpath也无法找到元素

时间:2018-08-16 07:17:35

标签: java selenium iframe

url = http://www.yopmail.com/en/

public class ReadEmailForEmailChange {

    WebDriver drive =  new FirefoxDriver();     
    drive.get("http://www.yopmail.com/en/");

    // I am able to get the size of frms
     int size = drive.findElements(By.tagName("iframe")).size();

     // this is the user name send keys.
     WebElement emai = (WebElement) drive.findElement(By.xpath("//input[@id='login']"));
     emai.sendKeys("testdenmark");

    // this is button to click. it is working as well
    WebElement emaenter = (WebElement)drive.findElement(By.xpath("//input[@title='Check inbox @yopmail.com']"));
    emaenter.click();

    // this is 1st frame. i am able to switch to this frame with no prob
    drive.switchTo().frame(drive.findElement(By.id("ifinbox")));


    // this is to find the email based on subject text. it is working fine
     WebElement emailopen = (WebElement) drive.findElement(By.xpath("//span[@class='lms' and text()='email verification']"));

     emailopen.click();      
     System.out.println("got to the emial.");

    // here comes problem. i am 100% that Id is correct or xpath is correct

但是我无法切换到此框架。我得到除了找不到元素

    //WebElement frame = drive.findElement(By.xpath("//tr//td//iframe[@id='ifmail']"));

    //drive.switchTo().frame(drive.findElement(By.id("ifmail")));
     //drive.switchTo().frame(drive.findElement(By.xpath("//iframe[@id='ifmail']")));

这会在我切换到相框之后发生

        drive.findElement(By.xpath("//a[text()='Verify email']")).click();

iframe是

<iframe class="whc" frameborder="0" scrolling="auto" id="ifmail" name="ifmail" src=""></iframe>

1 个答案:

答案 0 :(得分:0)

听起来您需要等待iframe首先出现。在尝试切换到IFrame之前,请尝试先等待网络驱动程序。

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ifmail")));