Selenium - 如何在 iframe 上定位元素

Selenium - How to locate elements on iframe

我正在尝试在 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[=11=](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[=11=]0(ExpectedConditions.java:44)
    at org.openqa.selenium.support.ui.ExpectedConditions.apply(ExpectedConditions.java:517)
    at org.openqa.selenium.support.ui.ExpectedConditions.apply(ExpectedConditions.java:513)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:248)
    ... 1 more

我也尝试过添加等待,但没有用。 Iframe 打开 post 单击按钮,我希望与 iframe 元素进行交互

您的代码似乎接近完美。但是,我会建议以下两个更改:

  • 当您寻找可用的 框架并切换到它时 更改 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");
    

通过使用 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