【问题标题】:Selenium - How to locate elements on iframeSelenium - 如何在 iframe 上定位元素
【发布时间】:2019-01-08 05:28:11
【问题描述】:

我想在 iframe 上查找元素。但我做不到。这是我得到的代码和错误。

这是我的脚本:

public class Add_Lists {

    public static void main (String []args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.get("http://automation.cloudaccess.host/administrator"); 

        driver.findElement(By.id("mod-login-username")).sendKeys("admin");

        driver.findElement(By.id("mod-login-password")).sendKeys("admin@123");

        driver.findElement(By.id("mod-login-password")).submit();

        driver.findElement(By.linkText("Content")).click();

        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.linkText("Articles"))).build().perform();

        driver.findElement(By.linkText("Add New Article")).click();

        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

        driver.findElement(By.linkText("Article")).click();

        Thread.sleep(5000);

        new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@src,'administrator/index.php')]")));
        WebElement filter = driver.findElement(By.id("filter_search"));

        filter.click();

        filter.sendKeys("Test");
    }

}

我得到的错误:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for frame to be available: By.xpath: //iframe[contains(@src,'administrator/index.php')] (tried for 20 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
    at testScripts.Add_Lists.main(Add_Lists.java:106)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //iframe[contains(@src,'administrator/index.php')]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
System info: host: 'vowellt4', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-24-generic', java.version: '1.8.0_171'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.ExpectedConditions.lambda$findElement$0(ExpectedConditions.java:896)
    at java.util.Optional.orElseThrow(Optional.java:290)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:895)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44)
    at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:517)
    at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:513)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:248)
    ... 1 more

我也尝试过添加等待,但没有帮助。点击按钮后 iframe 会打开,我希望与 iframe 元素进行交互

【问题讨论】:

标签: java selenium selenium-webdriver iframe webdriverwait


【解决方案1】:

您的代码看起来近乎完美。但是,我将建议如下两个更改:

  • 当您寻找 可用的框架并切换到它时更改src 属性,如下所示:

    new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@src,'http://automation.cloudaccess.host/administrator/index.php?')]")));
    
  • 一旦您切换到框架,当您搜索该框架内的元素时,会引发WebDriverWait,如下所示:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='filter_search']"))).sendKeys("Test");
    

【讨论】:

  • 谢谢。我还有一个场景,我有一个 iframe,里面有一个点击按钮,另一个 iframe 会打开。所以我应该和它一起表演吗,你能建议吗?
  • 我会提出一个新问题。谢谢!
【解决方案2】:

通过使用https://github.com/nick318/FindElementInFrames,您可以解决您的问题,例如:

SearchByFrames searchInFrame = searchFactory.search(() -> driver.findElement(By.xpath("//input[@id='filter_search']")));
WebElement elem = searchInFrame.getElem().orElseThrow();
elem.sendKeys("test");

所以你甚至不需要声明任何特定的 iframe

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    相关资源
    最近更新 更多