How do I wait for a JavaScript __doPostBack call?

1,229 views
Skip to first unread message

Debanjan Bhattacharjee

unread,
Mar 29, 2018, 12:56:36 PM3/29/18
to Selenium Users
Hello Everyone,

I am facing a rare kind of issue while Automating through Selenium/Python while filling out two fields on a website. My script fills out the first field i.e. ORIGIN CITY pretty fine. I have induced WebDriverWait for the second field DELIVERY ADDRESS. My guess the DELIVERY ADDRESS field is pretty much clickable even before the waiter is induced.
But the ORIGIN CITY field have a JavaScript associated through onchange event as follows :

    onchange="javascript:setTimeout('__doPostBack(\'DrpCity\',\'\')', 0)"
    
Once the JavaScript finishes it clears up the text from the DELIVERY ADDRESS field.

I did had a look at the Java Client's  ExpectedConditions as jsReturnsValue which is not there for the Python client.

Any suggestions please?


My Code :

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("https://www.avis.co.in")
mySelect = Select(driver.find_element_by_id("DrpCity"))
mySelect.select_by_visible_text("Pune")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//input[@id='txtPickUp']"))).send_keys("XYZ")

Thanks and Regards
Debanjan-B

David

unread,
Mar 30, 2018, 12:54:38 PM3/30/18
to Selenium Users
Looks like __doPostBack is specific to ASP.NET and not pure javascript from doing some searching online?


While those posts are more developer centric functionality, what you could do for Selenium side is to inject JS code following those solutions to trigger something after the postback call (the Sys.WebForms.PageRequestManager solution) or have your developer add this for you, then in your selenium code you use javascript call to check for existence of this something you added after postback and until you find that, you keep polling/waiting for it via repeated javascript call + sleeps or use Expected conditions against something (a javascript variable value or state of a special hidden element).

For example in the after postback logic, you can set the value of a javascript variable, or set state on a hidden element and with selenium check condition of the value of the variable or state of the element and do not proceed further until it has changed (e.g. postback completed).

Debanjan Bhattacharjee

unread,
Apr 3, 2018, 8:38:56 AM4/3/18
to Selenium Users
@David, Thanks millions. The stackoverflow.com link which you provided gave a lot of insights regarding __doPostBack

The issue got solved following darrell grainger suggestion in the How do I wait for a JavaScript __doPostBack call? thread of  webdriver group.

Thanks everyone for your precious time.
Reply all
Reply to author
Forward
0 new messages