出现 module 'lib2to3.pgen2.driver' has no attribute 'find_element'的原因有很多,这里我只提供我运行时出现的错误情况,即解决办法。
原始代码:
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH, '//*[starts-with(@id,"s_user_name_menu"]/div/a[4]'))).click() self.driver.find_element_by_class_name('quit').click()
错误原因:
Traceback (most recent call last):
File "D:\python\p\lib\unittest\case.py", line 59, in testPartExecutor yield
File "D:\python\p\lib\unittest\case.py", line 615, in run testMethod()
File "D:\python\Testkuangjia\unittest\pandabus_unittest\pandabus_unittest\testcase\test_login_baidu.py", line 67, in test_loginBtn WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()
File "D:\python\p\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until value = method(self._driver)
File "D:\python\p\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 297, in __call__ element = visibility_of_element_located(self.locator)(driver)
File "D:\python\p\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 128, in __call__ return _element_if_visible(_find_element(driver, self.locator))
File "D:\python\p\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 411, in _find_element return driver.find_element(*by)
AttributeError: module 'lib2to3.pgen2.driver' has no attribute 'find_element'
即该模块中没有find_element属性的定义,
错误的原因是因为所导入的包不是自己所需要的包,即更不需要导入lib2to3.pgen2包(模块)
修改后的代码:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()
self.driver.find_element_by_class_name('quit').click()