Selenium, click on element, hangs
Asked Answered
H

4

8

This is about selenium webdriver in java. If clicking on an element, usually it goes fast but sometimes when server is busy it will say Connecting... at the top of the browser and hang. Usually to deal with waits, the code is: driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS); but in this case when the server hangs on a click(), this doesn't throw an exception after the time limit, since the webdriver does not start counting until the click finishes connecting to the next url. Did anyone deal with this before, and how?

Is there a way to time completion of click() and submit()?

Haroun answered 7/5, 2012 at 17:13 Comment(0)
L
4

Yes, this is a known problem and as of Selenium 2.21.0, there is a way to go around.

The problem is that implicit wait is designed to wait for unloaded elements when you search for some, but the click() method just waits until the browser states that the page is fully loaded.

Try driver.manage().timeouts().pageLoadTimeout() which is a new method in 2.21.0 and should deal exactly with this.

Lockard answered 7/5, 2012 at 20:4 Comment(4)
this works on firefoxdriver but not on chrome driver. most websites it works, but some it will take 10 seconds to throw the exception, even if timeout is 2 seconds which is still better than waiting 2 minutes.Haroun
I guess. For Chrome driver (and IEdriver, too), we'll just have to wait a little bit longer, it's a brand new method that didn't make it further than to FF. You know, Selenium 2 is still in vast development.Amanita
This makes it work always: FirefoxProfile fp = new FirefoxProfile(); fp.setPreference("webdriver.load.strategy", "unstable"); driver = new FirefoxDriver(fp); driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);Haroun
I am having this issue and I tried your theory using Selenium 2.39 and Firefox 26 and it did NOT resolve the problem and not with Chrome 31 either.Postmeridian
J
5

The Selenium documentation states that Click() blocks. If for any reason, Selenium believes the page isn't completely loaded, then your Click will hang your test.

I've found that the easiest work-around is to completely skip the click event and use:

element.SendKeys(Keys.Enter);

instead. You get a two for one special - it doesn't block AND you're testing accessibility since many impaired users only use the keyboard to navigate anyway.

Jongjongleur answered 11/9, 2015 at 17:27 Comment(2)
Or use 1 of 4 things. Fluent Wait, pageLoadTimeOuts, implicit waits, or a runnable/ Future if in Scala. This is very case dependent.Fool
Because not all of us know from selenium.webdriver.common.keys import KeysVeracruz
L
4

Yes, this is a known problem and as of Selenium 2.21.0, there is a way to go around.

The problem is that implicit wait is designed to wait for unloaded elements when you search for some, but the click() method just waits until the browser states that the page is fully loaded.

Try driver.manage().timeouts().pageLoadTimeout() which is a new method in 2.21.0 and should deal exactly with this.

Lockard answered 7/5, 2012 at 20:4 Comment(4)
this works on firefoxdriver but not on chrome driver. most websites it works, but some it will take 10 seconds to throw the exception, even if timeout is 2 seconds which is still better than waiting 2 minutes.Haroun
I guess. For Chrome driver (and IEdriver, too), we'll just have to wait a little bit longer, it's a brand new method that didn't make it further than to FF. You know, Selenium 2 is still in vast development.Amanita
This makes it work always: FirefoxProfile fp = new FirefoxProfile(); fp.setPreference("webdriver.load.strategy", "unstable"); driver = new FirefoxDriver(fp); driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);Haroun
I am having this issue and I tried your theory using Selenium 2.39 and Firefox 26 and it did NOT resolve the problem and not with Chrome 31 either.Postmeridian
B
1

When selenium hangs update your firefox version to be as updated as selenium

Biceps answered 25/2, 2017 at 18:18 Comment(0)
S
0

Another option to those listed above is to use a timeout funtion within a execute_script call e.g.

setTimeout(function () {document.querySelector('#button').click() }, 1000);

This causes selenium to return immediately and click happens without hanging, because selenium doesn't know a click was called.

Seer answered 5/6 at 2:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.