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

标签 python selenium selenium-webdriver xpath

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

当访问网站并且未检测到/显示第一个 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()

关于python - 如果无法定位元素,Selenium 继续执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55444044/

相关文章:

Python win32com.client.Dispatch 循环遍历 Word 文档并导出为 PDF;下一个循环发生时失败

python - 从 Swagger 编辑器生成 Swagger 服务器(Python Flask)时出错

java - 在 etsy.com 上找不到搜索按钮的定位器时遇到问题

java - Selenium chromeDriver 打开速度比直接在 chrome 浏览器中打开网站慢得多

python - 在 Selenium 中找不到项目时如何引发错误并转到其他功能

selenium - 使用 Selenium WebDriver 模拟 ENTER 或 RETURN 的最佳实践是什么

python - 如何在一个语句中从列表中删除多个项目?

c# - Selenium WebDriver C# - 获取文本

python - 具有特殊字符的 MYSQL::Connector/Python UPDATE 语句不起作用

python - 检查目标 : expected dense_49 to have 4 dimensions, 时出错,但得到形状为 (2250, 3) 的数组