find_elements_by_xpath returns None 递归调用后
find_elements_by_xpath returns None after recursive call
我有这样的东西:
def get_next_src(self):
if len(self.driver.find_elements_by_xpath(xpath/to/img)) > 0:
return self.driver.find_element_by_xpath(xpath/to/img).get_attribute("src")
else:
time.sleep(5) # wait until image is loaded
self.get_next_src() # recursive call
我检查我的 xpath
中是否有东西,然后我得到属性 src
。但是,由于我正在迭代虽然图库图像有时无法正确加载,所以我必须等待并重复调用。
现在,每当我进行递归调用时,它都会再次调用该函数 returns None
。但是如果我这样做而没有递归调用,只需重复上面的 if
语句:
def get_next_src(self):
if len(self.driver.find_elements_by_xpath(xpath/to/img)) > 0:
return self.driver.find_element_by_xpath(xpath/to/img).get_attribute("src")
else:
time.sleep(5) # wait until image is loaded
# repeat the if statement from above
if len(self.driver.find_elements_by_xpath(xpath/to/img)) > 0:
return self.driver.find_element_by_xpath(xpath/to/img).get_attribute("src")
我正确地得到了 src
标签。
为什么在重复if
语句时递归调用得到None
并得到正确的src
属性?
def get_next_src(self):
if len(self.driver.find_elements_by_xpath(xpath/to/img)) > 0:
return self.driver.find_element_by_xpath(xpath/to/img).get_attribute("src")
else:
time.sleep(5) # wait until image is loaded
return self.get_next_src() # recursive call
尝试做 return self.get_next_src(),在你的情况下你只是调用函数。
我有这样的东西:
def get_next_src(self):
if len(self.driver.find_elements_by_xpath(xpath/to/img)) > 0:
return self.driver.find_element_by_xpath(xpath/to/img).get_attribute("src")
else:
time.sleep(5) # wait until image is loaded
self.get_next_src() # recursive call
我检查我的 xpath
中是否有东西,然后我得到属性 src
。但是,由于我正在迭代虽然图库图像有时无法正确加载,所以我必须等待并重复调用。
现在,每当我进行递归调用时,它都会再次调用该函数 returns None
。但是如果我这样做而没有递归调用,只需重复上面的 if
语句:
def get_next_src(self):
if len(self.driver.find_elements_by_xpath(xpath/to/img)) > 0:
return self.driver.find_element_by_xpath(xpath/to/img).get_attribute("src")
else:
time.sleep(5) # wait until image is loaded
# repeat the if statement from above
if len(self.driver.find_elements_by_xpath(xpath/to/img)) > 0:
return self.driver.find_element_by_xpath(xpath/to/img).get_attribute("src")
我正确地得到了 src
标签。
为什么在重复if
语句时递归调用得到None
并得到正确的src
属性?
def get_next_src(self):
if len(self.driver.find_elements_by_xpath(xpath/to/img)) > 0:
return self.driver.find_element_by_xpath(xpath/to/img).get_attribute("src")
else:
time.sleep(5) # wait until image is loaded
return self.get_next_src() # recursive call
尝试做 return self.get_next_src(),在你的情况下你只是调用函数。