How do I wait for a JavaScript __doPostBack call?

478 views
Skip to first unread message

Debanjan Bhattacharjee

unread,
Mar 29, 2018, 12:57:09 PM3/29/18
to webdriver
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:49:20 PM3/30/18
to webdriver
Looks to be cross posted here: https://groups.google.com/forum/#!topic/selenium-users/IEam78gKH-Q.

Perhaps better to discuss in the other post.

darrell grainger

unread,
Mar 30, 2018, 1:24:20 PM3/30/18
to webdriver
If you get a WebElement then the DOM is refresh, the WebElement reference will be stale. So you can do the following:

  1. Go to https://www.avis.co.in
  2. elem1 = driver.find_element_by_id("txtPickUp")
  3. Select "Pune"
  4. WebDriverWait(driver, 10).until(EC.staleness_of(elem1))
  5. driver.find_element_by_id("txtPickUp").send_keys("XYZ")
What this is doing is, line 2 will create a reference to the DELIVER ADDRESS field. Line 3 will select the city of Pune. This will trigger the onchange attribute. The javascript will alter the DOM and cause elem1 to become stale. Line 4 will wait for elem1 to become stale.

Debanjan Bhattacharjee

unread,
Apr 3, 2018, 8:30:32 AM4/3/18
to webdriver
@Darrell, Thanks a lot.

Honestly, your suggestion amazes me every-time. As usual your suggestion worked.

I had to simply wait for  DELIVER ADDRESS to go stale and then invoke 'send_keys("XYZ")'

@David, Apologies for the cross-posting but I venture out here in this forum as you and @Darrell both are active in this den :)

Thanks and Regards
Debanjan-B
Reply all
Reply to author
Forward
0 new messages