在 PyCharm 中使用 EventFiringWebDriver
调试 Selenium
脚本,导致记录异常:
'WebDriver' object has no attribute '__len__'
它会减慢 IDE 但不会破坏(停止)脚本。
完整代码如下:
from selenium import webdriver
from selenium.webdriver.support.event_firing_webdriver import EventFiringWebDriver
from selenium.webdriver.support.events import AbstractEventListener
class ScreenshotListener(AbstractEventListener):
def on_exception(self, exception, driver):
print(exception)
driver = webdriver.Chrome('chromedriver.exe')
ef_driver = EventFiringWebDriver(driver, ScreenshotListener())
ef_driver.get('https://google.com/')
title = ef_driver.title
driver.quit()
assert "Google", title
当我在标准模式下运行附加脚本时,一切正常。
但是使用 PyCharm Debug 运行(这里是调试控制台开始的输出):
C:\Pro\selenium-debug\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 59668 --file C:/Pro/selenium-debug/simple_test.py
我遇到了这些异常(每次我调用 ef_driver
时):
'WebDriver' object has no attribute '__len__'
'WebDriver' object has no attribute '__len__'
它不会停止脚本,只会导致调试器变慢(它会更新驱动程序并将焦点设置到浏览器)。
知道为什么它会被触发和记录吗?
奖励:使用 pdb
在控制台中运行调试是可以的,所以这看起来像严格的 PyCharm Debug 连接的东西
最佳答案
看起来 PyCharm 调试检查对象是否具有 __len__
属性:
elif hasattr(v, "__len__")
并且对象 WebDriver
没有 __len__
。
一个解决方案可能是(从下面的链接)用 try\catch 来扭曲:
try:
if is_numeric_container(type_qualifier, typeName, v):
xml_shape = ' shape="%s"' % make_valid_xml_value(str(v.shape))
elif hasattr(v, "__len__") and not is_string(v):
try:
xml_shape = ' shape="%s"' % make_valid_xml_value("%s" % str(len(v)))
except:
pass
except KeyError:
pass
关于python - 在 PyCharm 中调试时在 EventFiringWebDriver 中触发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66799693/