是否可以在 .until 中调用 WebDriver 外部的函数?无论我尝试什么,我都会得到异常:
Exception: 'WebDriver' object has no attribute 'verifyObj_tag'.
我有一个名为“ad_selenium”的类,所有对 selenium 的调用都封装在库中。我写的 explicitWait 函数试图在 .until 中使用另一个类方法:
def explicitWait(self,tag_name,search_for,element=None,compare='contains',seconds=20):
element = WebDriverWait(self.__WD, seconds).until( lambda self: \
self.verifyObj_tag(tag_name,search_for,element=element,compare=compare))
我尝试了 lambda 函数和函数变量的各种组合,例如:
def explicitWait(self,tag_name,search_for,element=None,compare='contains',seconds=20):
x = self.verifyObj_tag
element = WebDriverWait(self.__WD, seconds).until( lambda x: \
x(tag_name,search_for,element=element,compare=compare))
看selenium/webdriver/support/wait.py里面的代码,好像总是把webriver传给until传入的方法:
def until(self, method, message=''):
while(True):
try:
value = method(self._driver) #<<--webdriver passed here
if value:
return value
except self._ignored_exceptions:
pass
关于如何让它发挥作用有什么想法吗?
最佳答案
你需要让它作为参数传递driver
:
element = WebDriverWait(self.__WD, seconds).until(lambda driver: \
self.verifyObj_tag(tag_name, search_for, element=element, compare=compare))
关于python - selenium - WebDriverWait().until(myFunc) 可以使用 WebDriver 之外的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27426600/