Timeout on Selenium click event when action is opening a new window

Andrew Nicholas
Andrew Nicholas used Ask the Experts™
on
I'm writing some selenium automation using c# in Visual Studio with NUnit and Chrome browser. In the webpage there is a Yes button which opens a new window.

The webpage states - Do you wish to proceed Yes / No?

Answer [Yes] a new window opens and then another new window within that has a [Print] button

The button has some javascript associated with it:  onclick="return printId();"
function printId() 
{
	var orgName=$("#organisationId option:selected").html();
	var siteName=$("#siteId option:selected").html();
    $("#orgName").text(orgName);
    $("#siteName").text(siteName);
    var container = $('#svgDiv');
	$('#svgDiv').hide();
    var productId=$('#packProductId').val();
    var printWindow = window.open('', '', 'height=400,width=800');
    printWindow.document.write('<html><head><title></title>');
    printWindow.printerStatus=function(){
    	 	   	   	   afterPrintSetFlag();
    };
    
    printWindow.document.write('</head><body  onafterprint="printerStatus();"><body  onafterprint="printerStatus();">');
    console.log($('#barcode').JsBarcode(productId));
    printWindow.document.write($(container).html());
    printWindow.document.write('</body></html>');
    printWindow.document.close();
    printWindow.print();
    printWindow.close();
}

Open in new window


When the [Yes] button is clicked in Selenium the new window opens but the click event does not complete, the code hangs on the click and then throws a timeout error after 60 seconds. It looks like the new window opens and completes i.e. there is nothing to indicate the new window is still loading, nothing is spinning. To resolve this i enclosed the click in a try catch to handle the error. However the next section of code then times out after 60 seconds as well.

By complete chance i left this code running on the try-catch in debug and found that if i sleep for 9 minutes (so thats 10 minutes in total including the 60 second click timeout), then the code continues without any further timeouts. If i reduce the sleep time the timeout failure re-occurs.
try
{
    //This click works but doesnt complete, and then throws a timeout error after 60 seconds and falls into the catch
    DialogYesBtn.Click();

}
catch
{
    Thread.Sleep(540000); //9  minutes if i drop this to 8 minutes then the next step also times out
}

// this works with the above timeouts - if i dont sleep  in the catch this times out after 60 seconds as well 
SeleniumDriver.Instance.SwitchTo().Window(SeleniumDriver.Instance.WindowHandles.Last());

//unable to find this element
PrintBtn.Click(); 

Open in new window

Any idea what is happening here? I need to click a button in the last window but Selenium cant seem to find them (I guessing because its went stale as 10 minutes has elapsed)
I need a efficient solution to the code thats currently been written then waiting 10 minutes for some sort of time out to occur.
Am i right in thinking that the javascript onclick function opens the window and waits for the user to click the Print button which then would complete the [Yes] click action i.e. its impossible to automate in its current state?

I put the following section of debug code before the PrintBtn click to loop through each window to see what elements are available but none are found in any window.
//finds 3 windows, the original and the 2 that opened onclick
List<string> windows = SeleniumDriver.Instance.WindowHandles.ToList();
foreach (var handle in windows)
{

    SeleniumDriver.Instance.SwitchTo().Window(handle);

    //none of these elements are found in any window
    var elems1 = SeleniumDriver.Instance.FindElements(By.Id("header"));
    var elems2 = SeleniumDriver.Instance.FindElements(By.Id("button-strip"));
    var elems3 = SeleniumDriver.Instance.FindElements(By.TagName("control-button"));
    var elems4 = SeleniumDriver.Instance.FindElements(By.ClassName("action-button"));

}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
catch(e)
{
    alert(e); // <- what do you get in the alert?
    Thread.Sleep(540000); //9  minutes if i drop this to 8 minutes then the next step also times out
}

Open in new window

Andrew NicholasNon Functional Tester

Author

Commented:
Thanks

catch(Exception e):
The HTTP request to the remote WebDriver server for URL http://localhost:49476/session/009e204ed7cb3eb9088dc5be08a7dc0f/element/0.9077736631000759-13/click timed out after 60 seconds.
leakim971Multitechnician
Top Expert 2014

Commented:
google "The HTTP request to the remote WebDriver server for URL" this is a commun selenium error
basically it seems you need to use a different Chrome driver or initialize it differently :

ChromeOptions options = new ChromeOptions();
options.EnableMobileEmulation(deviceName);
options.AddArgument("no-sandbox");

ChromeDriver drv = new ChromeDriver(ChromeDriverService.CreateDefaultService(), options, TimeSpan.FromMinutes(3));
drv.Manage().Timeouts().PageLoad.Add(System.TimeSpan.FromSeconds(30));  

Open in new window

Andrew NicholasNon Functional Tester

Author

Commented:
Thanks  - I've tried that but all it does is extend the timeout to TimeSpan.FromMinutes(3) and it then fails. The issue is that the [Yes] click event is not completing. I think its because there is a javascript onclick event on that element which waits for the [Print] button to be clicked in the new window before completing. The problem is i cant automate the click of the Print button when the Yes click event is still in progress.
Non Functional Tester
Commented:
Having debugged the javascript further i dont think there is a solution within Selenium. The javascript onclick event creates a new window with a print button:

var printWindow = window.open('', '', 'height=400,width=800');
printWindow.print();

Open in new window


I then hangs on the printWindow.print(); waiting for the print button to be pressed. The [Yes] click event does not complete until this is done. Its not possible to do a Click() on the Print button within the click event for the Yes button.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial