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

在带有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()

提前致谢。

回答:

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

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

以上是 Selenium wait.until检查ajax请求完成是否抛出错误 的全部内容, 来源链接: utcz.com/qa/408717.html

回到顶部