dor*_*ino 5 python ajax selenium
因此,我有一个 Web 应用程序需要收集信息并构建一些报告并运行一些基本的数据分析。
问题是我完全是 HTML、Ajax(异步 JavaScript 和 XML)、Python 和 Selenium 的新手。
到目前为止,我收集的是:
Wait操作的异步事件:
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')))`
Run Code Online (Sandbox Code Playgroud)
EC 代表预期条件,表示为:
标题_是;
标题_包含;
Presence_of_element_located
visibility_of_element_located
可见性_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
driver.implicitly_wait(10)——使用什么,因为我有以下情况:
通过 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>
Run Code Online (Sandbox Code Playgroud)
这是按钮的事件:
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
}))
}
然后,我的网络通知 ajaxCoach 继续执行以下请求
是否可以使用 selenium 查看/查找 AJAX 操作是否结束了 Python 中的页面实现操作?
小智 1
如果页面上有jquery,则可以用jquery定义按钮并等待事件函数准备好。对于你的问题:
driver.execute_script('button = $("#div_39_1_3");')
events = driver.execute_script('return $._data(button[0],
"events");')
Run Code Online (Sandbox Code Playgroud)
现在您需要等到 events 变量不为 none。