首页 > 解决方案 > 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。我不知道如何解决这个问题!任何提示将不胜感激!

标签: seleniumweb-crawler

解决方案


试试下面的代码

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

推荐阅读