在 Python 中使用 Angular JS(量角器)和 Selenium

Using Angular JS(Protractor) with Selenium in Python

我正在尝试 select 使用 selenium 包装在 angular 1 中的文本区域,但是它在 DOM 中看不到。有一个名为 Pytractor 的模块。我一直在尝试解决这个问题,但我无法正确使用它。

谁能帮我解决这个问题?

您还可以使用常规 selenium 绑定来测试 AngularJS 应用程序。您需要使用 Explicit Waits 来等待元素出现、消失、title/url 更改等 - 对于任何可以让您继续测试页面的操作。

示例(等待 textarea 元素出现):

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

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.TAG_NAME, "myaccount")))

pytractor(如 protractor 本身)提供了一件重要的事情 - 它知道 AngularJS 何时结算并准备就绪 - 模型已更新,没有未完成的异步请求等。这并不意味着您必须使用它来测试 AngularJS 应用程序,但它会给您带来优势。

此外,pytractor 为您提供了新的定位器,例如您可以按模型或绑定查找元素。这也不意味着您无法使用 regular selenium python provides out-of-the-box 等其他定位技术找到相同的元素。

请注意,pytractor 目前没有积极开发和维护。

您可能只是从量角器中挖掘有用的代码片段。我个人使用此功能,直到 Angular 完成页面渲染。

def wait_for_angular(self, selenium_driver):
    selenium_driver.set_script_timeout(10)
    selenium_driver.execute_async_script("""
        callback = arguments[arguments.length - 1];
        angular.element('html').injector().get('$browser').notifyWhenNoOutstandingRequests(callback);""")

'html' 替换为 'ng-app' 中的任何元素。

Angular 1 的解决方案来自 protractor/lib/clientsidescripts.js#L51. It could also be possible to adapt the solution to Angular 2 using this updated code