Python/Selenium webdriver 中的 ElementNotVisibleException 错误
Posted
技术标签:
【中文标题】Python/Selenium webdriver 中的 ElementNotVisibleException 错误【英文标题】:ElementNotVisibleException error in Python/Selenium webdriver 【发布时间】:2018-07-30 01:29:30 【问题描述】:我为网页抓取工作编写了这段代码:
browser.find_element_by_class_name('open_all_j').click()
此代码行给我一个错误:
selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见
我的完整代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common import keys, action_chains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as soup
import xlwt
def click_time():
browser = webdriver.Chrome("./Drivers/chromedriver.exe")
browser.implicitly_wait(20)
browser.get("https://www.geegeez.co.uk/race-cards/#display=cards&day=0")
timeTable = browser.find_elements_by_class_name('meeting')
timeRow = timeTable[0].find_element_by_class_name("races")
timeRowTable = timeRow.find_elements_by_class_name("race_card_race")
timeRowTD = timeRowTable[0].find_elements_by_xpath("//table")
x = timeRowTable[0].find_element_by_class_name("cardstable")
y = x.find_element_by_class_name("racetime")
y.click()
print('\n', len(browser.find_elements_by_id('tabs-cards')), '\n')
wait = WebDriverWait(browser, 20)
elem = wait.until(EC.presence_of_element_located((By.ID, "tabs-cards")))
#browser.find_element_by_xpath("//div[@class = 'open_all_r']").click() #find_element_by_class_name('open_all_r')
browser.find_elements_by_class_name('open_all_j')[0].click()
browser.find_elements_by_class_name('open_all_t')[0].click()
我尝试使用:
browser.find_element_by_xpath("//div[@class = 'open_all_r']").click()
这个代码行给了我同样的错误。
请帮帮我...
【问题讨论】:
嗨,它工作正常,它没有抛出任何错误,它完全点击了目标元素。 可能元素没有滚动到视图中您可以尝试将元素滚动到视图中吗? 【参考方案1】:由于许多原因,例如元素未滚动到视图中,可能会发生这种情况
driver.execute_script("arguments[0].scrollIntoView();", element)
如果没有帮助,请尝试 java script click
driver.execute_script("arguments[0].click();", element)
【讨论】:
这个元素不需要滚动到视图中,它清晰可见,你可以检查它,因为他也给出了url。以及为什么在将元素拉入视图后需要通过 JS 单击一次? @Rajagopalan Java 脚本单击仅在滚动无法解决问题时作为替代方法。 我是说您不必滚动,因为它清晰可见,打开您可以看到的那个网站。【参考方案2】:首先,尝试捕捉这是什么类型的异常。这很可能是由于 2 个原因 - “原因 1”:可能是页面未正确滚动。 "solution" : 使用 "javascriptexecutor" 滚动页面(解决方案由上面其他人提供。 “原因 2”:可能是页面没有完全加载(我也陷入了这个问题)。在这种情况下,解决方案是您必须通过在线程中使用 sleep 方法等待页面完全加载。 thread.sleep(5000)
【讨论】:
他也给出了网址。您可以加载并查看这是什么类型的问题,而不是推测它。我加载了该站点,发现目标元素显然已准备好触发任何操作。【参考方案3】:由于多种原因,当您尝试点击某个元素时,可能到那时它可能不可见/不可点击。
我克服这些情况的方法是为 FindElement 和 FindElements 提供这样的扩展方法(代码在 c# 中,您可以在 python 中编写等价物):
public static void FindElement(this IWebDriver driver, By by, int timeout)
if(timeout >0)
return new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until(ExpectedConditions.ElementToBeClickable(by));
return driver.FindElement(by);
public static IReadOnlyCollection<IWebElement> FindElements(this IWebDriver driver, By by, int timeout)
if(timeout >0)
return new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(by));
return driver.FindElements(by);
你可以这样称呼它:
driver.FindElement(By.Xpath("xpath"), timeout).Click();
【讨论】:
以上是关于Python/Selenium webdriver 中的 ElementNotVisibleException 错误的主要内容,如果未能解决你的问题,请参考以下文章
python+selenium自动测试之WebDriver的常用API(基础篇一)
python+selenium+webdriver+BeautifulSoup实现自动登录