selenium - Selenium Web-scraping:selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互
问题描述
from selenium import webdriver
from bs4 import BeautifulSoup
from openpyxl import load_workbook
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import pandas as pd
from pandas import ExcelWriter
driver = webdriver.Chrome('./chromedriver')
driver.implicitly_wait(5)
driver.get('http://car.bitauto.com/')
# step 2
elem = driver.find_element_by_id('cx-searchBox')
elem.click()
wait = WebDriverWait(driver, 10)
brandlist = [brand.text for brand in wait.until(EC.visibility_of_all_elements_located((By.CLASS_NAME, 'yccmp-brand-item ')))]
brands2 = ["".join(brand[1:].lstrip()) for brand in brandlist]
for brand in brands2:
driver.get('http://car.bitauto.com/')
elem = driver.find_element_by_id('cx-searchBox')
elem.click()
elem.send_keys(brand)
# The error occurs at this part
driver.implicitly_wait(5)
elem.send_keys(Keys.RETURN)
driver.implicitly_wait(4)
上面的代码是通过在搜索栏输入品牌键从网站获取数据。出于某种原因, elem.send_keys(brand) 部分会导致错误说 element not intractable。我不知道如何解决这个问题!任何提示将不胜感激!
解决方案
试试下面的代码
for brand in brands2:
driver.get('http://car.bitauto.com/')
elem = driver.find_element_by_id('cx-searchBox')
elem.click()
elem.send_keys(brand)
# The error occurs at this part
element = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, "// input[ @ id = 'yccmp-searchBtn']")))
element.click()
注意:将以下导入添加到您的解决方案中
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
推荐阅读
- javascript - Nodejs TypeError 无法读取未定义 req.body 的属性“加入”
- javascript - NodeJS,多个 if 可能会返回标头(并且失败)
- corda - 我们可以在 Contract.verify() 中设置/更新 ContractState 的属性吗?
- android - 尝试在空对象引用上调用接口方法“int android.security.IKeystoreService.del(java.lang.String, int)”
- html - 将带有新行的文本复制到不能在 IE 中工作
- c - 如何组合 fputs 而不会引发错误
- python - 从具有不同信息的多个文本文件中读取值
- javascript - 获取部分视图模型数组
- c# - 当我将焦点移到它时打开 ComboBox
- c++ - Yaml-cpp:在发射器中的每个新地图之前禁用自动 begindoc (---)