selenium报错“ElementNotInteractableException: Message: element not interactable: Element is not currently visible”

1、源代码:

from selenium import webdriver
import time as t
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.baidu.com")
t.sleep(3)
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").send_keys(Keys.ENTER)
# 显式等待,待元素定位
WebDriverWait(driver,10).until(ec.presence_of_element_located((By.ID,"kw")))
# 鼠标悬停
mouse = driver.find_element_by_name("tj_settingicon")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text("搜索设置").click()
# 隐式等待
driver.implicitly_wait(5)
# 定位下拉框
select = driver.find_element_by_name("NR")
# # 以索引的方式定位
# Select(select).select_by_index(2)
# print("下拉框选择的最新条数是:",select.get_attribute('value'))
# 以value定位
# Select(select).select_by_value("10")
# print("下拉框选择的最新条数是:",select.get_attribute('value'))
# 以文本定位
Select(select).select_by_visible_text("每页显示20条")
print("下拉框选择的最新条数是:",select.get_attribute('value'))
t.sleep(3)
driver.quit()

报错:“ElementNotInteractableException: Message: element not interactable: Element is not currently visible”意思是元素隐藏不可见

百度了很多方法,但是都没有解决此问题

加了sleep(3),然后就ok了

再取消sleep,也没有报错了

posted @ 2020-04-03 10:42  Penny悦  阅读(2286)  评论(0编辑  收藏  举报