How do I wait for a JavaScript __doPostBack call through Selenium and WebDriver

别说谁变了你拦得住时间么 提交于 2019-11-27 02:13:43

I have found, the easiest way to deal with these JS postbacks, is to wait for the element that is affected by the load to go stale or not be found.

Here is an example function:

def waitForElementRemoved(Element, WaitCount, WaitTime):
    ElementRemoved = False
    WaitTry = 0
    while not ElementRemoved:
        try:
            if WaitTry > WaitCount:
                raise Exception("Element not removed from page in alloted time")
            Test = Element.text
            WaitTry += 1
            time.sleep(WaitTime)
        except (NoSuchElementException, StaleElementReferenceException):
            ElementRemoved = True

And then I would pick an element that is affected by this postback load and pass it to the function along with some timing arguments.

such as:

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

I don't know if this is the best way to deal with JavaScript onchange load events, but in my case, it has been very effective.


To add on to this answer: I have found that waiting until element goes stale does not always work when there are multiple load events, I have found that using that method along with using the below code works much more effectively to make sure these dynamic load events are complete: (I adapted the below code from HERE)

# Wait for AJAX (Jquery or JS) dynamic page load events
class DynamicLoadState:
    def __call__(self, driver):
        LoadComplete = False
        JQueryLoadComplete = False
        JSLoadComplete = False
        try:
            if driver.execute_script("return jQuery.active") == 0: JQueryLoadComplete = True
        except Exception:
            # JQuery is not present on page
            JQueryLoadComplete = True
        if driver.execute_script("return document.readyState") == 'complete': JSLoadComplete = True
        if JQueryLoadComplete and JSLoadComplete: LoadComplete = True
        return LoadComplete

def WaitForDynamicLoad(driver, WaitTime):
    WebDriverWait(driver, WaitTime).until(DynamicLoadState())

# Use the first method of waiting for the element to go stale
# then run this to make sure all loading is completed

WaitForDynamicLoad(driver, Counts.WaitTime)

Hope this helps keep someone from using time.sleep() in the future for page load!

@PixelEinstein 's Answer is the working and accepted Answer as his idea to wait for the element that is affected by the load to go stale or not be found did the trick. Finally I have arrived to a solution through a much simpler way then it was expected. I had to simply wait for the DELIVER ADDRESS field to go stale and then invoke 'send_keys("XYZ")' again on DELIVER ADDRESS field as follows :

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.staleness_of(driver.find_element_by_xpath("//input[@id='txtPickUp']")))
driver.find_element_by_xpath("//input[@id='txtPickUp']").send_keys("XYZ")

The discussion How to wait on the __doPostBack method to complete in javascript? was helpful to understand __doPostBack()

tl;dr

Sys.WebForms.PageRequestManager endRequest Event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!