Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium try:返回字符串,但不包括:返回布尔值_Python_Selenium_If Statement_Boolean_Try Catch - Fatal编程技术网

Python Selenium try:返回字符串,但不包括:返回布尔值

Python Selenium try:返回字符串,但不包括:返回布尔值,python,selenium,if-statement,boolean,try-catch,Python,Selenium,If Statement,Boolean,Try Catch,我有一个类似的代码: def number_of_doors(): try: return WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".number .doors"))).text except: return False def number_of_windows(): try: return W

我有一个类似的代码:

def number_of_doors():
   try:
      return WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".number .doors"))).text
   except:
      return False

def number_of_windows():
   try:
      return WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".number .windows"))).text
   except:
      return False

if number_of_doors() == number_of_windows():
   print('Doors and windows matched')
elif number_of_doors() != number_of_windows():
   print('Doors and windows not matched')
elif number_of_doors() is False:
   print('Doors not found')

有人告诉我,在try中使用字符串,在except中使用布尔值是一种不好的做法?如果是这样的话,还有什么更好(正确)的解决方案呢?

中,您可以简单地返回一个空字符串,而不是布尔值False,除了:
我认为在这种情况下您根本不应该使用try/catch。只需返回元素的文本

def number_of_doors():
    WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".number .doors")))
    return driver.find_element_by_css_selector(".number .doors").text


def number_of_windows():
    WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".number .windows")))
    return driver.find_element_by_css_selector(".number .windows").text


if number_of_doors() == number_of_windows():
    print('Doors and windows matched')
elif number_of_doors() != number_of_windows():
    print('Doors and windows not matched')
如果未找到元素,
WebDriverWait
将引发
TimeoutException

您的代码也有缩进问题。

我认为测试函数本身的返回值会更简单。如果您希望它不为None或Null,那么以这种方式进行测试会容易得多:

从selenium.webdriver导入WebDriverWait
导入selenium.common.exceptions
def门的数量()
尝试:
doors=WebDriverWait(驱动程序,5)。直到(位于((By.CSS_选择器,“.number.doors”))的_元素的EC.visibility_)。文本
windows=WebDriverWait(驱动程序,5)。直到(位于((By.CSS\u选择器,“.number.windows”))的元素的EC.visibility\u)。文本
如果门不是无,窗不是无:
#返回true或false,并测试它们是否匹配。您可以根据需要对此进行调整,但这将检查两个值是否都包含任何内容。
返回{
“成功”:没错,
“它们匹配吗”:门=窗
}
其他:
#因为我们只关心成功中的真值,所以将其返回为false,并将它们匹配为none类型。
返回{
“成功”:错误,
“他们匹配吗”:无,
“错误”:无,
“错误消息”:无
}
例外(selenium.common.exceptions,Exception)为e:
#返回一个dict对象,并能够提取出您绝对需要的值。错误信息、成功值以及它们是否匹配均为无,因为未测试任何内容。
返回{
“成功”:错误,
“他们匹配吗”:无,
“错误”:正确,
“错误消息”:“{}”。格式(e)
}
ax=门的数量()
如果ax['Success']:
#如果成功是真的,那就提炼出我们需要的价值观。
打印(“Success:{}\n它们是否匹配?{}”。格式(ax['Success'],ax['Do_他们匹配]]))
elif ax['Success']为假:
#检查它是否为false,是否存在错误。
如果ax['Error']和len(ax['Error\u Message'])!=0:
#如果dict对象中存在错误,那么我们可以找出错误所在。
error=ax['error\u Message']
其他:
错误=无
打印(“成功:{}\n错误:{}”。格式(ax['Success'],错误))

如果找不到门或窗,是否希望继续执行代码?是的,无论我希望我的代码继续执行。谢谢你的建议,但我忘了说,如果找不到元素,我希望继续执行代码,而不是抛出TimeoutException。好的,你找到解决方案了吗?嗯,这不是唯一明显的解决方案(如果我想避免布尔值)是在中使用字符串,如:except:return'Error'谢谢,是的,现在看来这是最相关的答案,所以我只是将except:return False改为except:return'Error'(例如)