Python 函数调用时只是跳过执行-但没有调试器错误

Python 函数调用时只是跳过执行-但没有调试器错误,python,python-2.7,selenium,selenium-webdriver,selenium-chromedriver,Python,Python 2.7,Selenium,Selenium Webdriver,Selenium Chromedriver,我调试了代码,当程序到达这一行代码(一个更大的函数)时 …它会跳到下一行(没有错误或任何东西) 我试着运行它,但是输出了相同的结果 以下是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_ch

我调试了代码,当程序到达这一行代码(一个更大的函数)时

…它会跳到下一行(没有错误或任何东西)

我试着运行它,但是输出了相同的结果

以下是
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\u 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)

最有可能的是,在顶部将登录结果设置为1,它永远找不到元素,并且直接返回1,我的答案可能不会给出您想要的解决方案,但我会尝试一下。您没有说明在从函数login_结果中获取返回值后要执行什么检查。如果没有断言返回值,我想这就是为什么代码会继续执行。函数中没有引发异常,它被设计为返回并让用户对结果进行处理

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

即时调试对我来说总是很奇怪,所以脚本语言中的断点总是给我带来问题。我想您也只是在函数中抛出了一个打印输出,以确保调试器没有问题?是的,登录结果会更改。那一定是这样。没想到这个调试器会这么乱。你对一个好的调试器有什么建议吗?不幸的是,没有。Python有它自己的插件,这可能是你最可靠的赌注,但使用它有点困难。是的,条件也有问题,但主要问题来自调试器。它跳过了函数的执行,尽管我在函数中创建了断点。我已经修复了if语句,现在它按预期运行。谢谢@我明白了,谢谢你的澄清!是的,我忘记提了。返回值由if语句处理。问题来自调试器…它被窃听了。
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