问题
I am having a weird issue that when I click to open the popup window then code freezes for exactly 10-12 minutes and then interacts normally with popup window. Following is complete code:
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);
Above code freezes after clicking on "Print Change" for 10-12 min and clicks on "Print" button on new pop-up window.
OS: Windows 10; Browser: Chrome 77.0.3865.90; Selenium 3.141.59
Any help would be appreciated.
Regards, Surender
回答1:
The problem is with window.showModalDialog() set it in to window.open by using js.
((JavascriptExecutor) driver).executeScript("window.showModalDialog = window.open;");
OR
use this js click, it will solve the problem
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);
来源:https://stackoverflow.com/questions/58376666/selenium-script-hangs-when-popup-window-is-opened-and-runs-normally-after-10-min