Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.6.2/Selenium 2.0/AJAX - How to wait for script to conclude server request

So, I have a Web application at work that need to gather information and build some reports and run some basic data analysis.

The thing is that I'm a complete newbie to HTML, Ajax (Asynchronous JavaScript and XML), Python and Selenium.

What I gather so far is this:

  1. Ajax nature is to perform asynchronous Web Browser activities and in my case, sending server requests to push/pull some data
  2. Selenium handles the asynchronous events performing Wait actions like:
    • time.sleep('time in ms') # using the time library. So not REALLY Selenium;
    • Explicit Waits: you define to wait for a certain condition to occur before proceeding further in the code;

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    delay_time = 10 # how much time until raises NoExeption in Selenium    

    driver = webdriver.Firefox()
    driver.get("http://somedomain/url_that_delays_loading")

    webDriverWait(driver,delay_time)\
    .until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))`

EC stands for expected conditions represented by:

title_is;

title_contains;

presence_of_element_located

visibility_of_element_located

visibility_of

presence_of_all_elements_located

text_to_be_present_in_element

text_to_be_present_in_element_value

frame_to_be_available_and_switch_to_it

invisibility_of_element_located

element_to_be_clickable

staleness_of

element_to_be_selected

element_located_to_be_selected

element_selection_state_to_be

element_located_selection_state_to_be

alert_is_present



  • Implicit Waits: tell WebDriver to poll the DOM (Document Object Model) for a certain amount of time when trying to find an element or elements if they are not immediately available; driver.implicitly_wait(10)-
  • Executing JavaScript using Java and applies wait: j Query keeps a count of how many Ajax calls are active in its query.active variable;
  • FluentWait: FluentWait option to handle uncertain waits;
  • WebdriverWait: use ExpectedCondition and WebDriverWait strategy.

What to use since I have the following situation:

Button to send a clear request via Ajax.

<div id="div_39_1_3" class="Button  CoachView CPP BPMHSectionChild CoachView_show" data-type="com.ibm.bpm.coach.Snapshot_b24acf10_7ca3_40fa_b73f_782cddfd48e6.Button" data-binding="local.clearButton" data-bindingtype="boolean" data-config="config175" data-viewid="GhostClear" data-eventid="boundaryEvent_42" data-ibmbpm-layoutpreview="horizontal" control-name="/GhostClear"> 
    <button class="btn btn-labeled"><span class="btn-label icon fa fa-times"></span>Clear</button></div>

This is the event of the button: function(a) {!e._instance.btn.disabled && c.ui.executeEventHandlingFunction(e, e._proto.EVT_ONCLICK) && (e._instance.multiClicks || (e._instance.btn.disabled = !0, f.add(e._instance.btn, "disabled")), e.context.binding && e.context.binding.set("value", !0), e.context.trigger(function(a) { e._instance.btn.disabled = !1; f.remove(e._instance.btn, "disabled"); setTimeout(function() { c.ui.executeEventHandlingFunction(e, e._proto.EVT_ONBOUNDARYEVT, a.status) }) }, { callBackForAll: !0 })) }

Then, my network informs that the ajaxCoach proceeds to the following requests

Networkflow for the clear button

Is it possible to selenium to see/find if an AJAX action concluded the page actualization action in Python?

like image 379
dormanino Avatar asked Nov 07 '22 16:11

dormanino


1 Answers

if you have jquery on the page, you can define the button with jquery and wait until the event function is ready. for your question:

    driver.execute_script('button = $("#div_39_1_3");')
    events = driver.execute_script('return $._data(button[0], 
    "events");')

now you need to wait until events variable is not none.

like image 110
davidbrown Avatar answered Nov 14 '22 21:11

davidbrown