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)部分导致错误,提示元素不可处理。我不知道该如何解决!任何提示将不胜感激!
答案 0 :(得分:1)
尝试下面的代码
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