【发布时间】:2020-01-13 11:29:55
【问题描述】:
我是基于 AJAX 的应用程序,上面有一些框架,想切换到它,因此我使用了frameToBeAvailableAndSwitchToIt() 方法。
假设现在我在一个页面的默认上下文中(即不在任何框架中)并且想使用以下代码切换到 testFrame -
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(testFrame));
JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
String currentFrameName = (String) jsExecutor.executeScript("return self.name");
System.out.println("***************** Current frame using self.name " + currentFrameName);
这将返回空字符串作为 currentFrameName 的值,这表明我们尚未切换到 testFrame,即使显式等待调用已成功。
此问题间歇性重现,导致测试不稳定。您能否建议如何使其稳定?
编辑
我在前面提到的地方编辑了我的问题 - 我在 frameA,然后想切换到 frameB,但事实并非如此。我想从页面的默认内容切换到框架。很抱歉将错误信息放在首位。
【问题讨论】:
-
你可以试试 --> String frame = (String) jsExecutor.executeScript("return self.location.toString()");
-
@Muzzamil,它返回给我一个网址。
-
我想你已经切换了。切换后试试这个 --> WebElement testFrame= driver.findElement(By.tagName("iframe"));WebElement el = (WebElement) ((JavascriptExecutor) driver).executeScript("return window.frameElement");断言 testFrame.equals(el)
标签: selenium selenium-webdriver