Link to home
Start Free TrialLog in
Avatar of Andrew Nicholas
Andrew NicholasFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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

Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

Avatar of Andrew Nicholas

ASKER

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.
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

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.
ASKER CERTIFIED SOLUTION
Avatar of Andrew Nicholas
Andrew Nicholas
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial