【问题标题】:Using Angular JS(Protractor) with Selenium in Python在 Python 中使用带有 Selenium 的 Angular JS(量角器)
【发布时间】:2015-06-12 17:50:52
【问题描述】:

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

谁能帮我解决这个问题?

【问题讨论】:

    标签: python angularjs selenium selenium-webdriver protractor


    【解决方案1】:

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

    示例(等待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 目前并未积极开发和维护。

    【讨论】:

      【解决方案2】:

      您可以只挖掘量角器以获得有用的代码 sn-ps。我个人使用这个函数会阻塞直到 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。也可以使解决方案适应 Angular 2 using this updated code

      【讨论】:

      • 我必须使用 document.getElementsByTagName('html') 来避免错误“jqLit​​e 不支持通过选择器查找元素!”
      • 这对我有用,我也喜欢使用量角器的想法。不错的收获?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多