我调试了代码,当程序到达这行代码(功能更大的代码)时......
login_result = login(driver)
...它会跳到下一行(没有错误或其他任何内容)
我试过运行它但输出的结果相同。
以下是login()
功能:
def login(driver):
# Check if logged in (or login failed)
login_result = 1
login_check_elements = driver.find_elements_by_tag_name('button')
for login_check in login_check_elements:
if(login_check.text == "Log in" and login_check.is_enabled()):
# If there is a login button on the screen and the button is enabled by default it means
# that the driver is on the account page and is not logged in
login_result = 0
# If login failed - return 1
if(login_result == 1):
return 1
else:
username, password = find_input_elements(driver)
username.send_keys("username")
time.sleep(randint(1, 3))
password.send_keys("password")
time.sleep(randint(1, 3))
login_button = find_login_button(driver)
login_button.click()
return 0
*编辑:我忘了说,是的,我在函数的几乎每一行都包含了断点,包括第一行。
答案 0 :(得分:0)
我的猜测是检查元素的行在其列表中没有任何内容。
for login_check in login_check_elements:
再次尝试运行代码,并检查login_result的值是什么
def login(driver):
# Check if logged in (or login failed)
login_result = 1
login_check_elements = driver.find_elements_by_tag_name('button')
for login_check in login_check_elements:
print('element found')
if(login_check.text == "Log in" and login_check.is_enabled()):
# If there is a login button on the screen and the button is enabled by default it means
# that the driver is on the account page and is not logged in
login_result = 0
# If login failed - return 1
if(login_result == 1):
return 1
else:
username, password = find_input_elements(driver)
username.send_keys("username")
time.sleep(randint(1, 3))
password.send_keys("password")
time.sleep(randint(1, 3))
login_button = find_login_button(driver)
login_button.click()
return 0
login_result = login(driver)
print(login_result)
最有可能的方法是将login_result设置为顶部的1,它永远不会找到元素,直接返回1
答案 1 :(得分:0)
我的回答可能无法提供您正在寻找的解决方案,但我会抓住它。从函数login_result获取返回值后,您尚未说明执行了哪些检查。如果没有声明返回值,我猜这就是为什么你的代码只是继续执行。并且在函数中没有引发异常,它旨在返回并让用户对结果执行某些操作
def login(driver):
# Check if logged in (or login failed)
login_check_elements = driver.find_elements_by_tag_name('button')
for login_check in login_check_elements:
if login_check.text == "Log in" and login_check.is_enabled():
# If there is a login button on the screen and the button is enabled by default it means
# that the driver is on the account page and is not logged in
return False
else:
username, password = find_input_elements(driver)
username.send_keys("username")
time.sleep(randint(1, 3))
password.send_keys("password")
time.sleep(randint(1, 3))
login_button = find_login_button(driver)
login_button.click()
return True
login_result = login(driver)
if not login_result:
# Error out or do something