gpt4 book ai didi

python - 如何避免在 selenium quit() 上获得 `' NoneType' object has no attribute 'path' `?

转载 作者:太空狗 更新时间:2023-10-30 01:05:58 24 4
gpt4 key购买 nike

当运行 Selenium Webdriver Python 脚本时,在执行 self.driver.quit() 后得到一个 'NoneType' object has no attribute 'path'
try/except 中包含 self.driver.quit() 没有帮助,即:

$ cat demo_NoneType_attribute_error.py
# -*- coding: utf-8 -*-
from selenium import webdriver
import unittest

class TestPass(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()

def test_pass(self):
pass

def tearDown(self):
print("doing: self.driver.quit()")
try:
self.driver.quit()
except AttributeError:
pass

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

$ python demo_NoneType_attribute_error.py
doing: self.driver.quit()
'NoneType' object has no attribute 'path'
.
----------------------------------------------------------------------
Ran 1 test in 19.807s

OK

$

有没有人知道如何避免 'NoneType' object has no attribute 'path' 消息?

注意:
由于此问题已在 11 月初报告(请参阅下面的 URL),它现在应该有一个补丁 - 但是将 seleniumpip 升级到最新版本并没有消除

环境:Selenium 3.0.2; Python 2.7; Windows 7 上的 Cygwin 32 位。

最佳答案

这似乎是 selenium 3.0 版本中的一个错误

更新firefoxwebdriver.py中的quit()方法定义如下(相对路径:..\Python27\Lib\site-packages\selenium\webdriver\firefox\webdriver.py):

更改 quit() 方法中的以下行:

shutil.rmtree(self.profile.path) #which gives Nonetype has no attribute path
if self.profile.tempfolder is not None:
shutil.rmtree(self.profile.tempfolder)

if self.profile is not None:
shutil.rmtree(self.profile.path) # if self.profile is not None, then only rmtree method is called for path.
if self.profile.tempfolder is not None:
shutil.rmtree(self.profile.tempfolder) # if tempfolder is not None, then only rmtree is called for tempfolder.

注意:凡使用self.profile的地方,做同样的事情。即,如上所述将代码移动到 if 条件。


Selenium 3.0 中,profilebinary 移动到 firefox_options 而不是它们作为 单独存在Selenium 2.0 中的 firefox_profilefirefox_binary

您可以在 __init__ 方法中的 webdriver.py(firefox)中验证这一点。

__init__方法中的相关代码:

if firefox_options is None:
firefox_options = Options()
print dir(firefox_options) # you can refer binary and profile as part of firefox_options object.

注意:观察到 firefox_options.profile 仍然给出 None,这可能是一个需要在 selenium 3.0

中修复的问题

关于python - 如何避免在 selenium quit() 上获得 `' NoneType' object has no attribute 'path' `?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40998945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com