小编典典

Selenium wait.until检查ajax请求完成是否抛出错误

selenium

在带有Python的Selenium
Webdriver中,我想等待Ajax请求完成(jquery库)。我使用Selenium的wait.until()函数。单击submitJquery按钮后,Ajax请求开始。

wait.until(self.driver.execute_script("return jQuery.active == 0"))

但我得到以下错误:

E
======================================================================
ERROR: test_MahsumAkbasNet_Pass (__main__.TestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\xxx\src\unittestpackage\JavaScriptExec.py", line 24, in test_MahsumAkbasNet_Pass
    wait.until(self.driver.execute_script("return jQuery.active == 0"))
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 66, in until
    value = method(self._driver)
TypeError: 'bool' object is not callable

----------------------------------------------------------------------
Ran 1 test in 14.449s

FAILED (errors=1)

完整的代码是:

# -*- coding: UTF-8 -*-
import unittest
import time
import datetime
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

class TestClass(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.set_page_load_timeout(30)
        self.driver.maximize_window()

    def test_MahsumAkbasNet_Pass(self):
        wait = WebDriverWait(self.driver, 30)

        self.driver.get("http://www.mahsumakbas.net/selenium")

        self.driver.find_element_by_id("submitJquery").click()

        wait.until(self.driver.execute_script("return jQuery.active == 0"))

        print "Jquery is completed"

    def tearDown(self):
        self.driver.close()

if __name__ == '__main__':
    unittest.main()

提前致谢。


阅读 444

收藏
2020-06-26

共1个答案

小编典典

您需要将 可调用对象 传递给wait.until()

wait.until(lambda driver: driver.execute_script("return jQuery.active == 0"))
2020-06-26