我無法弄清楚這段代碼中的錯誤在哪里。基本上它在搜索欄中插入一個代碼,單擊一個按鈕并提取結果:
from seleniumwire import webdriver
import time
API_KEY = 'my_api_key'
proxy_options = {
'proxy': {
'https': f'http://scraperapi:{API_KEY}@proxy-server.scraperapi.com:8001',
'no_proxy': 'localhost,127.0.0.1'
}
}
url = 'https://www.ufficiocamerale.it/'
vats = ['06655971007', '05779661007', '08526440154']
for vat in vats:
driver = webdriver.Chrome(seleniumwire_options=proxy_options)
driver.get(url)
time.sleep(5)
item = driver.find_element_by_xpath('//form[@id="formRicercaAzienda"]//input[@id="search_input"]')
item.send_keys(vat)
time.sleep(1)
button = driver.find_element_by_xpath('//form[@id="formRicercaAzienda"]//p//button[@type="submit"]')
button.click()
time.sleep(5)
all_items = driver.find_elements_by_xpath('//ul[@id="first-group"]/li')
for item in all_items:
if '@' in item.text:
print(item.text.split(' ')[1])
driver.close()
運行腳本(chromedriver.exe 保存在同一個檔案夾中,我在 Jupyter Notebook 中作業,如果重要的話)我得到
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//form[@id="formRicercaAzienda"]//input[@id="search_input"]"}
但是這個元素存在,因為在沒有 ScraperAPI 的情況下嘗試腳本我沒有得到任何錯誤。誰能弄清楚問題是什么?
uj5u.com熱心網友回復:
- 在這里,您正在運行 3 個
vat值的回圈。
第一次單擊搜索按鈕后,將顯示結果頁面。
那里沒有搜索輸入欄位和搜索按鈕!
因此,為了執行新的搜索,您需要在獲取結果頁面上的資料后回傳上一頁。 - 無需每次迭代都創建新的 Web 驅動程式實體。
- 此外,您應該使用預期條件顯式等待而不是硬編碼暫停。
這應該會更好:
from seleniumwire import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
API_KEY = 'my_api_key'
proxy_options = {
'proxy': {
'https': f'http://scraperapi:{API_KEY}@proxy-server.scraperapi.com:8001',
'no_proxy': 'localhost,127.0.0.1'
}
}
url = 'https://www.ufficiocamerale.it/'
vats = ['06655971007', '05779661007', '08526440154']
driver = webdriver.Chrome(seleniumwire_options=proxy_options)
wait = WebDriverWait(driver, 20)
driver.get(url)
for vat in vats:
input_search = wait.until(EC.visibility_of_element_located((By.XPATH, '//form[@id="formRicercaAzienda"]//input[@id="search_input"]')))
input_search.clear()
input_search.send_keys(vat)
time.sleep(0.5)
wait.until(EC.visibility_of_element_located((By.XPATH, '//form[@id="formRicercaAzienda"]//p//button[@type="submit"]'))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, '//ul[@id="first-group"]/li')))
time.sleep(0.5)
all_items = driver.find_elements_by_xpath('//ul[@id="first-group"]/li')
for item in all_items:
if '@' in item.text:
print(item.text.split(' ')[1])
driver.execute_script("window.history.go(-1)")
driver.close()
UPD
此代碼有效,輸出為
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436003.html
標籤:Python 硒 硒网络驱动程序 网页抓取 硒铬驱动程序
