Python Selenium - 尝试单击链接时出现 'ElementNotVisibleException' 错误

标签 python selenium webdriver

我想做的就是:转到“http://news.google.com”,然后单击侧面菜单上的“技术”链接。这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest, time, re

class GoogleTech(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.com/"
        self.verificationErrors = []
        self.accept_next_alert = True


    def test_googletech(self):
        driver = self.driver
        driver.get("http://news.google.com")
        #driver.find_element_by_xpath("//div[@id='main-wrapper']/div[@id='main-pane']/div[@class='background']/div[@class='centered']/div[@id='nav-menu-wrapper']/div[@class='browse-sidebar']/ul[@id='anchorman-two-browse-nav']/li[@class='nav-item nv-en_us:tc']/a[contains(text(),'Technology')]").click()
        #driver.find_element_by_xpath("//ul[@id='anchorman-two-browse-nav']/li/a[contains(text(),'Technology')]").click()
        #driver.find_element_by_xpath("//ul[@id='anchorman-two-browse-nav']/li[@class='nav-item nv-en_us:tc']/a[contains(text(),'Technology')]").click()
        #driver.find_element_by_xpath("html/body/div[3]/div[1]/div/div/div[2]/div/ul/li[6]/a").click()
        #driver.find_element_by_link_text("Technology").click()
        wait = WebDriverWait(driver, 10)
        link = wait.until(EC.presence_of_element_located((By.XPATH, "//ul[@id='anchorman-two-browse-nav']/li/a[contains(text(),'Technology')]")))
        link.click()
        time.sleep(10)
        ...

我已经尝试了很多组合(所有注释掉的行加上更多!),但仍然无法让它工作。在将其输入代码之前,我已检查以确保我正在使用的元素确实存在(使用元素检查器)。但即使是“find_element_by_link_text”也给了我一个 NoSuchElementException 异常。有人可以告诉我我做错了什么吗?

更新:经过进一步的测试,我现在可以更好地了解 news.google.com 页面何时发生此错误。因此,基本上,包含“技术”链接的侧面菜单被设置为可滚动(我相信这个错误只是因为链接位于可滚动区域而发生)。如果浏览器(Selenium 脚本在运行时打开)不显示“技术”链接,则会显示错误。您必须最大化浏览器或向下滚动才能看到链接。您可以自己测试此错误 - 当测试打开浏览器时,快速调整浏览器窗口大小,以便不显示“技术”链接,并且测试将失败。如果您的初始浏览器窗口在首次打开时不显示“技术”链接,则除非您最大化浏览器窗口或快速向下滚动直到显示“技术”链接,否则它将失败。该错误可能有两种形式 - 如果您使用 wait.until 方法,您将收到 TimeoutException。如果您不使用 wait.until 方法,您将看到 ElementNotVisibleException。我已经在 Chrome 和 Firefox 中进行了测试,对于这两种浏览器,我都看到了这个问题。我是 Selenium 的新手,但我相信这不是正常行为,有人可以确认吗?如果这正常,那么有人可以告诉我如何确保我的测试每次都正确运行吗?

最佳答案

我认为您想使用 element_to_be_clickable 作为您的 ExpectedCondition

这意味着您的驱动程序将继续轮询 Selenium,直到发现该元素可见并且已启用 source

尝试以下解决方案

 link = wait.until(EC.element_to_be_clickable((By.XPATH, "//ul[@id='anchorman-two-browse-nav']/li/a[contains(text(),'Technology')]")))

 link.click()
 time.sleep(10)

关于Python Selenium - 尝试单击链接时出现 'ElementNotVisibleException' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26007556/

相关文章:

java - 使用 Gecko 驱动程序运行我的 Selenium 脚本时出现 "org.openqa.selenium.WebDriverException: Unsupported Marionette protocol version 2"错误

python - 函数没有等待预定的时间 - Python Selenium

python - 对按某些属性过滤的多个 docker 容器日志运行 grep 查询

python - 将数据框列与条件进行比较

python - 如何从 selenium webdriver 元素中提取链接?

selenium - WebElement.clear() 对文本框做了什么?

python - 带有 python 的 Selenium webdriver-如果加载时间太长,如何重新加载页面?

python - 不使用 kv 语言创建 Kivy 小部件?

python - 我无法在 vscode 中运行代码或调试 python 文件

python - Selenium Webdriver Python 查找下一个元素