首页 > 解决方案 > 在 Python 中使用 Selenium 的元素不可交互错误

问题描述

我正在尝试从网站上抓取一些数据,并且每隔一段时间就会出现一个弹出窗口。我正在尝试构建一个功能,该功能将通过按“x”图标来删除弹出窗口。我尝试了多种单击它的方法,但每次我似乎都得到相同的“元素不可交互”错误。

以下是我的 3 种不同尝试:

    try:
        if driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/a').is_displayed():
            wait = WebDriverWait(driver, 10)
            element = wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div[1]/a")))
            driver.execute_script("arguments[0].click();", element)
    except Exception as e:
        print(e)
try:
        if driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/a').is_displayed():
            element = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/a')
            ActionChains(driver).move_to_element(element).click().perform()
    except Exception as e:
        print(e)
try:
        if driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/a').is_displayed():
            element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[text()='x']")))
            driver.execute_script("arguments[0].click();", element)
    except Exception as e:
        print(e)

html 代码并没有说“x”是一个按钮,而是说它是位于标签内的文本。我对html不是很熟悉,所以如果你需要它来帮助我可以提供。

非常感谢

编辑:这是我试图按下的按钮的 html 代码:

<a data-trigger="dismiss.close" style="background:rgb(0,0,0);border-radius:100%;box-shadow:rgba(0,0,0,.6) 0 2px 6px;color:rgb(255,255,255);font-family:sans-serif;font-size:20px;font-weight:400;display:block;height:32px;line-height:32px;position:absolute;right:-15px;text-align:center;top:-15px;width:32px;cursor:pointer;bottom:auto;left:auto;z-index:99;min-width:0;max-width:none;min-height:0;max-height:none;">×</a>

标签: pythonseleniumselenium-webdriverclickselenium-chromedriver

解决方案


推荐阅读