强制 Selenium 等待 AngularJS [英] Force Selenium to wait for AngularJS
问题描述
如何强制 python Selenium 等待一秒钟,直到 AngularJS 完成页面解析并加载一些它需要的东西.
How can I force python Selenium wait for a second until AngularJS completes page parsing and loads some stuff that it needs.
或者如何强制 Selenium 在单击按钮后等待 1 秒,这会导致对服务器的 ajax 请求,由 AngularJS 处理.我需要在导航到其他页面之前执行服务器端操作.
Or how can I force Selenium to wait for 1 second after button click, which causes ajax request to the server, handled by AngularJS. I need server side actions to take place before navigating to other page.
推荐答案
如果您的应用程序中的某些异步行为非常重要以至于真正的用户应该等待它,那么您应该告诉他们.同样,您的脚本可以在继续之前等待相同的指示.
If some asynchronous behavior in your application is important enough that a real user should wait for it, then you should tell them. Similarly, your script can wait for that same indication before proceeding.
例如,如果用户单击触发 API 调用以创建记录的按钮,并且用户需要等待该记录创建,您应该向他们显示一条消息,指示何时成功完成,例如,记录创建成功."然后,您的脚本可以像用户一样等待相同的文本出现.
For example, if a user clicks a button that triggers an API call to create a record, and the user needs to wait for that record to be created, you should show them a message indicating when it completes successfully, e.g., "Record created successfully." Your script can then wait for that same text to appear, just as a user would.
重要的是,您的应用程序的实现方式无关紧要.重要的是您的用户可以使用您的应用程序,而不是它调用某些 AngularJS API 或 React API 等.
Importantly, it shouldn't matter how your application is implemented. What matters is that your users can use your application—not that it calls certain AngularJS APIs or React APIs, etc.
Selenium 包括 WebDriverWait
和 expected_conditions
模块帮助您等待特定条件的满足:
Selenium includes WebDriverWait
and the expected_conditions
module to help you wait for particular conditions to be met:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
TIMEOUT = 5
# ...
WebDriverWait(driver, TIMEOUT).until(
EC.text_to_be_present_in_element(
[By.CLASS_NAME, "alert"],
"Record created successfully"))
2.使用 Capybara(使用 Selenium)
正如你在上面看到的,裸硒是复杂和挑剔的.capybara-py 抽象了大部分内容:
from capybara.dsl import page
# ...
page.assert_text("Record created successfully")
这篇关于强制 Selenium 等待 AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!