我将打开一个包含iframe的对话框,然后在打开对话框后将焦点设置在iframe中。
目前,我尝试在加载内容时将重点放在iframe上:
<iframe src="../credits.html" onload="this.contentWindow.focus()"></iframe>
除了Firefox之外,它适用于所有浏览器,我不明白为什么。
有人可以告诉我为什么吗? 感谢
答案 0 :(得分:4)
Firefox似乎没有contentWindow属性,你应该使用contentDocument
<强>更新强>:
经过一番小小的辩论后,我想出了一个更好的解决方案:
<html>
<head>
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$(function() {
$("#xframe").bind("load", function(){
$(this).contents().find("#xa").focus();
});
});
</script>
</head>
<body>
<iframe id="xframe" src="./y.html"></iframe>
</body>
</html>
在这个例子中,我假设y.html中的输入字段有一个&#34; xa&#34; ID。它适用于Chrome和Firefox。
旧答案
更好的是你应该使用jquery,比如:
<html>
<head>
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#xframe").focus();
});
</script>
</head>
<body>
<iframe id="xframe" src="./y.html"></iframe>
</body>
</html>
当然你可以做一些额外的检查以确定iframe是否准备就绪。
可在此处找到更多信息http://www.w3schools.com/jsref/prop_frame_contentwindow.asp
答案 1 :(得分:1)
尝试使用jquery
执行此操作public static void flyerPageImageDetails(WebDriver driver) {
//switching to the flyer frame
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe.framePage")));
//To get the total no of pages
String pageText = getCurrentPageText(driver);
if (pageText != null && pageText.matches("\\d+.*of.*\\d+")) {
int pageCount = Integer.valueOf(pageText.substring(pageText.lastIndexOf(" ") + 1));
for (int i = 1; i <= pageCount; i++) {
sleepForASecond();
System.out.println("Printing Image Details for Page " + i);
System.out.println("--------------------------------------");
clickAllPointsInImage(driver);
//Moving to next image
driver.findElement(By.id("arrowNext")).click();
System.out.println("--------------------------------------");
}
} else {
clickAllPointsInImage(driver);
}
}
private static String getCurrentPageText(WebDriver driver) {
List<WebElement> elements = driver.findElements(By.xpath("//div[@id='slider']//span[@class='ttContent']"));
if (elements.isEmpty()) {
return null;
}
return elements.get(0).getText();
}
private static void clickAllPointsInImage(WebDriver driver) {
WebElement currentImageElement = getVisibleImageElementFromList(driver);
//Finding all the path elements for the visible image
List<WebElement> paths = currentImageElement.findElements(By.tagName("path"));
for (WebElement path : paths) {
try {
path.click();
//To get the title text
List<WebElement> title = driver.findElements(By.className("title-pd"));
if (!title.isEmpty()) {
System.out.println(title.get(0).getText());
}
driver.findElement(By.className("close")).click();
sleepForASecond();
} catch (Exception ex) {
}
}
}
private static WebElement getVisibleImageElementFromList(WebDriver driver) {
List<WebElement> imageElements = driver.findElements(By.xpath("//*[starts-with(@id,'span')]/div/div[1]/div[2]"));
for (WebElement imageElement : imageElements) {
if (imageElement.isDisplayed()) {
return imageElement;
}
}
return null;
}
private static void sleepForASecond() {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(DrangAndDrop.class.getName()).log(Level.SEVERE, null, ex);
}
}