gpt4 book ai didi

Python 3.6.2/Selenium 2.0/AJAX - 如何等待脚本结束服务器请求

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:39 29 4
gpt4 key购买 nike

因此,我有一个 Web 应用程序在工作,需要收集信息并构建一些报告并运行一些基本数据分析。

问题是我完全是 HTML、Ajax(异步 JavaScript 和 XML)、Python 和 Selenium 的新手。

到目前为止我收集到的是:

  1. Ajax 的本质是执行异步 Web 浏览器事件,在我的例子中,发送服务器请求以推送/拉取一些数据
  2. Selenium 处理执行 Wait 操作的异步事件,例如:
    • time.sleep('time in ms') # 使用时间库。所以不是真正的 Selenium ;
    • 显式等待:您定义等待特定条件发生,然后再继续执行代码;

    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



  • 隐式等待:告诉 WebDriver 在尝试查找一个或多个元素(如果它们不是立即可用的)时轮询 DOM(文档对象模型)一段时间;driver.implicitly_wait(10)-
  • 使用 Java 执行 JavaScript 并应用等待:j Query 在其 query.active 变量中记录有多少 Ajax 调用处于事件状态;
  • FluentWait:用于处理不确定等待的 FluentWait 选项;
  • WebdriverWait:使用 ExpectedCondition 和 WebDriverWait 策略。

我有以下情况怎么办:

用于通过 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>

这是按钮的事件: 函数(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, "禁用");
设置超时(功能(){
c.ui.executeEventHandlingFunction(e, e._proto.EVT_ONBOUNDARYEVT,
a.状态)
})
}, {
callBackForAll: !0
}))
}

然后,我的网络通知 ajaxCoach 继续执行以下请求

Networkflow for the clear button

selenium 是否可以查看/查找 AJAX 操作结束了 Python 中的页面实现操作?

最佳答案

如果你的页面有jquery,你可以用jquery定义按钮并等待事件函数准备就绪。针对您的问题:

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

现在您需要等到事件变量不为空。

关于Python 3.6.2/Selenium 2.0/AJAX - 如何等待脚本结束服务器请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241605/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com