Skip to main content
 首页 » 编程设计

python之如果无法定位元素,Selenium 继续执行脚本

2025年05月04日14bluestorm

我有一个问题,当我访问一个网站时,如果想检查元素是否显示和启用。如果是这样的话,我想打印一些东西。如果该元素未显示和启用,我想检查一个新元素,如果要打印某些内容,是否显示该元素。我希望你明白这一点。

当访问网站并且未检测到/显示第一个 IF 语句时,它给出错误而不是转到下一个 If 语句。

错误:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[@id="errorLongContent"] 

我已经尝试使用 try、expect 并将 if 语句更改为 elif。

我希望有人能帮助我。

from selenium import webdriver 
from selenium.webdriver import Firefox 
from selenium.webdriver.firefox.options import Options 
 
# Setup browser for 
options = Options() 
profile = webdriver.FirefoxProfile() 
driver = Firefox(executable_path='geckodriver', options=options, firefox_profile=profile) 
driver.get("https://agar.io") 
 
#Xpaths 
PLAY_BUTTON_XPATH = '//*[@id="play"]' 
PROXY_DENIES_CONNECTION = '//*[@id="errorLongContent"]' 
TIMEOUT_XPATH1 = '//*[@id="errorTryAgain"]' 
 
 
#checking for error 
def mainfunction(): 
    while True: 
        print("Starting") 
        if driver.find_element_by_xpath(PROXY_DENIES_CONNECTION).is_enabled() and driver.find_element_by_xpath( 
                PROXY_DENIES_CONNECTION).is_displayed(): 
            print("Proxy denies connection") 
            driver.quit() 
 
        if driver.find_element_by_xpath(TIMEOUT_XPATH1).is_enabled() and driver.find_element_by_xpath( 
                TIMEOUT_XPATH1).is_displayed(): 
            print("Time out detected") 
            driver.quit() 
 
        if driver.find_element_by_xpath(PLAY_BUTTON_XPATH).is_enabled() and driver.find_element_by_xpath( 
                PLAY_BUTTON_XPATH).is_displayed(): 
            print("Agar.io server is loaded") 
            break 
 
        else: 
            continue 
 
mainfunction() 

编辑:尝试并排除

def mainfunction(): 
    while True: 
        print("Starting") 
        try: 
            if driver.find_element_by_xpath(PROXY_DENIES_CONNECTION).is_enabled() and driver.find_element_by_xpath( 
                    PROXY_DENIES_CONNECTION).is_displayed(): 
                print("Proxy denies connection") 
                driver.quit() 
        except: 
            continue 
        try: 
            if driver.find_element_by_xpath(TIMEOUT_XPATH1).is_enabled() and driver.find_element_by_xpath( 
                    TIMEOUT_XPATH1).is_displayed(): 
                print("Time out detected") 
                driver.quit() 
        except: 
            continue 
 
        try: 
            if driver.find_element_by_xpath(PLAY_BUTTON_XPATH).is_enabled() and driver.find_element_by_xpath( 
                    PLAY_BUTTON_XPATH).is_displayed(): 
                print("Agar.io server is loaded") 
                break 
        except: 
            continue 

当我运行它时,它会在无限循环中运行,只打印开始...

请您参考如下方法:

我将您的 if 语句放在“try/except”语句中,它运行时没有出现上述错误。

from selenium import webdriver 
from selenium.webdriver import Firefox 
from selenium.webdriver.firefox.options import Options 
 
# Setup browser for 
options = Options() 
profile = webdriver.FirefoxProfile() 
driver = Firefox(executable_path='geckodriver', options=options, firefox_profile=profile) 
driver.get("https://agar.io") 
 
#Xpaths 
PLAY_BUTTON_XPATH = '//*[@id="play"]' 
PROXY_DENIES_CONNECTION = '//*[@id="errorLongContent"]' 
TIMEOUT_XPATH1 = '//*[@id="errorTryAgain"]' 
 
 
#checking for error 
def mainfunction(): 
    while True: 
        print("Starting") 
        try: 
            if driver.find_element_by_xpath(PROXY_DENIES_CONNECTION).is_enabled() and driver.find_element_by_xpath( 
                    PROXY_DENIES_CONNECTION).is_displayed(): 
                print("Proxy denies connection") 
                driver.quit() 
        except: 
            pass 
        try: 
            if driver.find_element_by_xpath(TIMEOUT_XPATH1).is_enabled() and driver.find_element_by_xpath( 
                    TIMEOUT_XPATH1).is_displayed(): 
                print("Time out detected") 
                driver.quit() 
        except: 
            pass 
        try: 
            if driver.find_element_by_xpath(PLAY_BUTTON_XPATH).is_enabled() and driver.find_element_by_xpath( 
                    PLAY_BUTTON_XPATH).is_displayed(): 
                print("Agar.io server is loaded") 
                break 
        except: 
            pass 
 
        else: 
            continue 
 
mainfunction()