打开弹出窗口 window 并在 10 分钟后正常运行时,Selenium 脚本挂起
Selenium script hangs when popup window is opened and runs normally after 10 minutes
我有一个奇怪的问题,当我点击打开弹出窗口 window 时,代码正好冻结 10-12 分钟,然后与弹出窗口 window 正常交互。以下是完整代码:
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir")+ "\exe\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
driver.get(prop.getProperty("https://genpact-qa-smartit.onbmc.com"));
driver.findElement(By.xpath("//*[@id='okta-signin-username']")).sendKeys(userid);
driver.findElement(By.xpath("//*[@id='okta-signin-password']")).sendKeys(pwd);
driver.findElement(By.xpath("//*[@id='okta-signin-submit']")).click();
driver.findElement(By.xpath("//a[@id='header-search_button']")).click();
driver.findElement(By.xpath("//input[@id='globalSearchBox']")).sendKeys("CRQ000000029504");
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
driver.findElement(By.xpath("//a[text()='View Full Change']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//div[@title='Print']")).click();
Thread.sleep(3000);
Actions a = new Actions(driver);
a.moveToElement(driver.findElement(By.xpath("//button[contains(text(),'Print Change')]")));
a.click();
a.perform();
Set<String> window_handles=driver.getWindowHandles();
String child=null;
System.out.println(window_handles.size());
for(String handle:window_handles) {
child=handle;
}
driver.switchTo().window(child);
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement print_button = (WebElement) js.executeScript("return document.querySelector('print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('#header').shadowRoot.querySelector('.action-button')");
print_button.click();
Thread.sleep(3000);
StringSelection ss = new StringSelection("Filename");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); //copy the above string to clip board
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
在单击 "Print Change" 10-12 分钟并单击新弹出窗口 "Print" 按钮后,以上代码冻结 window。
OS: Windows 10;浏览器:Chrome77.0.3865.90;硒 3.141.59
如有任何帮助,我们将不胜感激。
此致,
投降
问题是 window.showModalDialog() 使用 js 将其设置为 window.open。
((JavascriptExecutor) driver).executeScript("window.showModalDialog = window.open;");
或
用这个js点击,会解决问题
JavascriptExecutor js1 = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Print Change')]"));
js1.executeScript("var elem=arguments[0]; setTimeout(function() {elem.click();}, 100)", element);
我有一个奇怪的问题,当我点击打开弹出窗口 window 时,代码正好冻结 10-12 分钟,然后与弹出窗口 window 正常交互。以下是完整代码:
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir")+ "\exe\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
driver.get(prop.getProperty("https://genpact-qa-smartit.onbmc.com"));
driver.findElement(By.xpath("//*[@id='okta-signin-username']")).sendKeys(userid);
driver.findElement(By.xpath("//*[@id='okta-signin-password']")).sendKeys(pwd);
driver.findElement(By.xpath("//*[@id='okta-signin-submit']")).click();
driver.findElement(By.xpath("//a[@id='header-search_button']")).click();
driver.findElement(By.xpath("//input[@id='globalSearchBox']")).sendKeys("CRQ000000029504");
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
driver.findElement(By.xpath("//a[text()='View Full Change']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//div[@title='Print']")).click();
Thread.sleep(3000);
Actions a = new Actions(driver);
a.moveToElement(driver.findElement(By.xpath("//button[contains(text(),'Print Change')]")));
a.click();
a.perform();
Set<String> window_handles=driver.getWindowHandles();
String child=null;
System.out.println(window_handles.size());
for(String handle:window_handles) {
child=handle;
}
driver.switchTo().window(child);
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement print_button = (WebElement) js.executeScript("return document.querySelector('print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('#header').shadowRoot.querySelector('.action-button')");
print_button.click();
Thread.sleep(3000);
StringSelection ss = new StringSelection("Filename");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); //copy the above string to clip board
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
在单击 "Print Change" 10-12 分钟并单击新弹出窗口 "Print" 按钮后,以上代码冻结 window。
OS: Windows 10;浏览器:Chrome77.0.3865.90;硒 3.141.59
如有任何帮助,我们将不胜感激。
此致, 投降
问题是 window.showModalDialog() 使用 js 将其设置为 window.open。
((JavascriptExecutor) driver).executeScript("window.showModalDialog = window.open;");
或
用这个js点击,会解决问题
JavascriptExecutor js1 = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Print Change')]"));
js1.executeScript("var elem=arguments[0]; setTimeout(function() {elem.click();}, 100)", element);