正在尝试在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元素进行交互
答案 0 :(得分: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");
答案 1 :(得分:0)
通过使用 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