我调试了代码,当程序到达这行代码(更大的函数(时......
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
*编辑:我忘了说,是的,我在函数中的几乎每一行都包含了断点,包括第一行。
我的猜测是,检查元素的行在其列表中没有任何元素。
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
我的回答可能不会给出您正在寻找的解决方案,但我会尝试一下。您尚未说明从函数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