我无法切换到给定示例的模态对话框http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm我不知道如何在模态对话框上获取元素
uajslkp61#
用途以下方法切换到modelframe
driver.switchTo().frame("ModelFrameTitle");
或
driver.switchTo().activeElement()
希望这个能管用
2ledvvac2#
您使用的不是模型对话框,而是一个单独的窗口。使用此代码:
private static Object firstHandle;private static Object lastHandle;public static void switchToWindowsPopup() { Set<String> handles = DriverManager.getCurrent().getWindowHandles(); Iterator<String> itr = handles.iterator(); firstHandle = itr.next(); lastHandle = firstHandle; while (itr.hasNext()) { lastHandle = itr.next(); } DriverManager.getCurrent().switchTo().window(lastHandle.toString());}public static void switchToMainWindow() { DriverManager.getCurrent().switchTo().window(firstHandle.toString());
private static Object firstHandle;
private static Object lastHandle;
public static void switchToWindowsPopup() {
Set<String> handles = DriverManager.getCurrent().getWindowHandles();
Iterator<String> itr = handles.iterator();
firstHandle = itr.next();
lastHandle = firstHandle;
while (itr.hasNext()) {
lastHandle = itr.next();
}
DriverManager.getCurrent().switchTo().window(lastHandle.toString());
public static void switchToMainWindow() {
DriverManager.getCurrent().switchTo().window(firstHandle.toString());
sirbozc53#
Try the below code. It is working in IE but not in FF22. If找到模态对话框is printed in Console, then Modal dialog is identified and switched.
Try the below code. It is working in IE but not in FF22. If
is printed in Console, then Modal dialog is identified and switched.
public class ModalDialog { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub WebDriver driver = new InternetExplorerDriver(); //WebDriver driver = new FirefoxDriver(); driver.get("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm"); String parent = driver.getWindowHandle(); WebDriverWait wait = new WebDriverWait(driver, 10); WebElement push_to_create = wait.until(ExpectedConditions .elementToBeClickable(By .cssSelector("input[value='Push To Create']"))); push_to_create.click(); waitForWindow(driver); switchToModalDialog(driver, parent); } public static void waitForWindow(WebDriver driver) throws InterruptedException { //wait until number of window handles become 2 or until 6 seconds are completed. int timecount = 1; do { driver.getWindowHandles(); Thread.sleep(200); timecount++; if (timecount > 30) { break; } } while (driver.getWindowHandles().size() != 2); } public static void switchToModalDialog(WebDriver driver, String parent) { //Switch to Modal dialog if (driver.getWindowHandles().size() == 2) { for (String window : driver.getWindowHandles()) { if (!window.equals(parent)) { driver.switchTo().window(window); System.out.println("Modal dialog found"); break; } } } } }
public class ModalDialog {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new InternetExplorerDriver();
//WebDriver driver = new FirefoxDriver();
driver.get("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm");
String parent = driver.getWindowHandle();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement push_to_create = wait.until(ExpectedConditions
.elementToBeClickable(By
.cssSelector("input[value='Push To Create']")));
push_to_create.click();
waitForWindow(driver);
switchToModalDialog(driver, parent);
public static void waitForWindow(WebDriver driver)
throws InterruptedException {
//wait until number of window handles become 2 or until 6 seconds are completed.
int timecount = 1;
do {
driver.getWindowHandles();
Thread.sleep(200);
timecount++;
if (timecount > 30) {
break;
} while (driver.getWindowHandles().size() != 2);
public static void switchToModalDialog(WebDriver driver, String parent) {
//Switch to Modal dialog
if (driver.getWindowHandles().size() == 2) {
for (String window : driver.getWindowHandles()) {
if (!window.equals(parent)) {
driver.switchTo().window(window);
System.out.println("Modal dialog found");
euoag5mw4#
R(R selenium )溶液:我有一个弹出对话框(这是动态生成的),因此在原始页面源代码中无法检测这里是为我工作的方法:方法1:模拟按Tab键并切换到该模态对话框我的当前键集中在模态对话框
remDr$sendKeysToActiveElement(list(key = "tab"))Sys.sleep(5)remDr$sendKeysToActiveElement(list(key = "enter"))Sys.sleep(15)
remDr$sendKeysToActiveElement(list(key = "tab"))
Sys.sleep(5)
remDr$sendKeysToActiveElement(list(key = "enter"))
Sys.sleep(15)
后面的下拉按钮方法2:如果你能找到框架(或iframe),将焦点移到它
date_filter_frame <- remDr$findElement(using = "tag name", 'iframe')date_filter_frame$highlightElement()Sys.sleep(5)remDr$switchToFrame(date_filter_frame)Sys.sleep(2)
date_filter_frame <- remDr$findElement(using = "tag name", 'iframe')
date_filter_frame$highlightElement()
remDr$switchToFrame(date_filter_frame)
Sys.sleep(2)
现在你可以在框架中搜索元素了。记住在命令之间放置足够的Sys.sleep,以便正确加载元素(以防万一)
date_filter_element <- remDr$findElement(using = "xpath", paste0("//*[contains(text(), 'Week to Date')]"))date_filter_element$highlightElement()
date_filter_element <- remDr$findElement(using = "xpath", paste0("//*[contains(text(), 'Week to Date')]"))
date_filter_element$highlightElement()
mrwjdhj35#
试试这个代码,包括你的对象名和变量工作.
Set<String> windowids = driver.getWindowHandles();Iterator<String> iter= windowids.iterator();for (int i = 1; i < sh.getRows(); i++){ while(iter.hasNext()){System.out.println("Main Window ID :"+iter.next());}driver.findElement(By.id("lgnLogin_UserName")).clear();driver.findElement(By.id("lgnLogin_UserName")).sendKeys(sh.getCell(0, i).getContents());driver.findElement(By.id("lgnLogin_Password")).clear();driver.findElement(By.id("lgnLogin_Password")).sendKeys(sh.getCell(1, i).getContents());driver.findElement(By.id("lgnLogin_LoginButton")).click();Thread.sleep(5000L); windowids = driver.getWindowHandles(); iter= windowids.iterator(); String main_windowID=iter.next(); String tabbed_windowID=iter.next(); System.out.println("Main Window ID :"+main_windowID); //switch over to pop-up window Thread.sleep(1000); driver.switchTo().window(tabbed_windowID); System.out.println("Pop-up window Title : "+driver.getTitle());
Set<String> windowids = driver.getWindowHandles();
Iterator<String> iter= windowids.iterator();
for (int i = 1; i < sh.getRows(); i++)
{
while(iter.hasNext())
System.out.println("Main Window ID :"+iter.next());
driver.findElement(By.id("lgnLogin_UserName")).clear();
driver.findElement(By.id("lgnLogin_UserName")).sendKeys(sh.getCell(0,
i).getContents());
driver.findElement(By.id("lgnLogin_Password")).clear();
driver.findElement(By.id("lgnLogin_Password")).sendKeys(sh.getCell(1,
driver.findElement(By.id("lgnLogin_LoginButton")).click();
Thread.sleep(5000L);
windowids = driver.getWindowHandles();
iter= windowids.iterator();
String main_windowID=iter.next();
String tabbed_windowID=iter.next();
System.out.println("Main Window ID :"+main_windowID);
//switch over to pop-up window
Thread.sleep(1000);
driver.switchTo().window(tabbed_windowID);
System.out.println("Pop-up window Title : "+driver.getTitle());
8ehkhllq6#
我试过了,对你很有效。
String mainWinHander = webDriver.getWindowHandle();// code for clicking button to open new window is ommited//Now the window opened. So here reture the handle with size = 2Set<String> handles = webDriver.getWindowHandles();for(String handle : handles){ if(!mainWinHander.equals(handle)) { // Here will block for ever. No exception and timeout! WebDriver popup = webDriver.switchTo().window(handle); // do something with popup popup.close(); }}
String mainWinHander = webDriver.getWindowHandle();
// code for clicking button to open new window is ommited
//Now the window opened. So here reture the handle with size = 2
Set<String> handles = webDriver.getWindowHandles();
for(String handle : handles)
if(!mainWinHander.equals(handle))
// Here will block for ever. No exception and timeout!
WebDriver popup = webDriver.switchTo().window(handle);
// do something with popup
popup.close();
holgip5t7#
假设预期只是弹出两个窗口(一个父窗口,一个弹出窗口),那么只需等待两个窗口出现,找到另一个窗口句柄并切换到它。
WebElement link = // element that will showModalDialog()// Click on the link, but don't wait for the document to finishfinal JavascriptExecutor executor = (JavascriptExecutor) driver;executor.executeScript( "var el=arguments[0]; setTimeout(function() { el.click(); }, 100);", link);// wait for there to be two windows and choose the one that is // not the original windowfinal String parentWindowHandle = driver.getWindowHandle();new WebDriverWait(driver, 60, 1000) .until(new Function<WebDriver, Boolean>() { @Override public Boolean apply(final WebDriver driver) { final String[] windowHandles = driver.getWindowHandles().toArray(new String[0]); if (windowHandles.length != 2) { return false; } if (windowHandles[0].equals(parentWindowHandle)) { driver.switchTo().window(windowHandles[1]); } else { driver.switchTo().window(windowHandles[0]); } return true; }});
WebElement link = // element that will showModalDialog()
// Click on the link, but don't wait for the document to finish
final JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(
"var el=arguments[0]; setTimeout(function() { el.click(); }, 100);",
link);
// wait for there to be two windows and choose the one that is
// not the original window
final String parentWindowHandle = driver.getWindowHandle();
new WebDriverWait(driver, 60, 1000)
.until(new Function<WebDriver, Boolean>() {
@Override
public Boolean apply(final WebDriver driver) {
final String[] windowHandles =
driver.getWindowHandles().toArray(new String[0]);
if (windowHandles.length != 2) {
return false;
if (windowHandles[0].equals(parentWindowHandle)) {
driver.switchTo().window(windowHandles[1]);
} else {
driver.switchTo().window(windowHandles[0]);
return true;
});
ibrsph3r8#
不,模型窗口需要由javaScriptExecutor处理,因为主要模型窗口由窗口模型组成,这将在模型出现后工作,然后控制进入模型并单击预期元素。必须导入javascriptexector如下所述,
Javascriptexecutor js =(Javascriptexecutor).driver;js.executescript(**<element to be clicked>**);
Javascriptexecutor js =(Javascriptexecutor).driver;
js.executescript(**<element to be clicked>**);
pes8fvy99#
pidoss. 1加上我的2美分,即使问题太老了
public void PressEnterKey() { var simulator = new InputSimulator(); simulator.Keyboard.KeyPress(VirtualKeyCode.RETURN); }
public void PressEnterKey()
var simulator = new InputSimulator();
simulator.Keyboard.KeyPress(VirtualKeyCode.RETURN);
你可以创建一个类似上面的方法,并在需要的地方调用它。pidoss. 2 -你也可以改变键盘输入(如向上箭头,向下箭头,向下翻页等)
9条答案
按热度按时间uajslkp61#
用途
以下方法切换到modelframe
或
希望这个能管用
2ledvvac2#
您使用的不是模型对话框,而是一个单独的窗口。
使用此代码:
sirbozc53#
Try the below code. It is working in IE but not in FF22. If
找到模态对话框is printed in Console, then Modal dialog is identified and switched.
euoag5mw4#
R(R selenium )溶液:我有一个弹出对话框(这是动态生成的),因此在原始页面源代码中无法检测这里是为我工作的方法:
方法1:模拟按Tab键并切换到该模态对话框我的当前键集中在模态对话框
后面的下拉按钮方法2:如果你能找到框架(或iframe),将焦点移到它
现在你可以在框架中搜索元素了。记住在命令之间放置足够的Sys.sleep,以便正确加载元素(以防万一)
mrwjdhj35#
试试这个代码,包括你的对象名和变量工作.
8ehkhllq6#
我试过了,对你很有效。
holgip5t7#
假设预期只是弹出两个窗口(一个父窗口,一个弹出窗口),那么只需等待两个窗口出现,找到另一个窗口句柄并切换到它。
ibrsph3r8#
不,模型窗口需要由javaScriptExecutor处理,因为主要模型窗口由窗口模型组成,这将在模型出现后工作,然后控制进入模型并单击预期元素。
必须导入javascriptexector
如下所述,
pes8fvy99#
pidoss. 1加上我的2美分,即使问题太老了
你可以创建一个类似上面的方法,并在需要的地方调用它。
pidoss. 2 -你也可以改变键盘输入(如向上箭头,向下箭头,向下翻页等)