我写了一个Python脚本,使用Selenium和ChromeDriver从网站(https://pemilu2019.kpu.go.id/#/ppwp/hitung-suara/)中抓取数据。该脚本在几个页面中导航,并单击各种按钮来检索数据。但是,我遇到了以下错误:
WebDriverException: Message: unknown error: unhandled inspector error: {"code":-32000,"message":"No node with given id found"}
错误似乎发生在迭代中的特定点,而不是随机的。我已经尝试解决这个问题,但我不知道是什么原因导致它或如何解决它。
我在Windows 10计算机上使用Python
3.10.5和Selenium
库(ChromeDriver
版本113.0.5672.63)。任何帮助解决这个问题将不胜感激。
我还是一个初学者,这是我第一次尝试 selenium 。我已经尝试添加time.sleep(1)
来确保web加载,检查元素的可见性,并且元素是可点击的,但问题仍然发生。
这是我目前写的剧本
url = 'https://pemilu2019.kpu.go.id/#/ppwp/hitung-suara/'
path = Service(r'...\chromedriver_win32')
options = Options()
options.add_experimental_option("debuggerAddress", "localhost:9222")
driver = webdriver.Chrome(service=path, options=options)
driver.get(url)
wait = WebDriverWait(driver, 10)
def scrape_left_table(prob, kab, kec):
data = []
rows = driver.find_elements(By.CSS_SELECTOR, 'div:nth-child(1) > table > tbody > tr')
for row in rows:
wilayah = row.find_element(By.CSS_SELECTOR, 'td.text-xs-left.wilayah-name > button').text
persentasi = row.find_element(By.CSS_SELECTOR, 'td.text-xs-left.wilayah-name > span').text
jokowi = row.find_element(By.CSS_SELECTOR, 'td:nth-child(2)').text
prabowo = row.find_element(By.CSS_SELECTOR, 'td:nth-child(3)').text
data.append([prob, kab, kec, wilayah, persentasi, jokowi, prabowo])
return data
def scrape_right_table(prob, kab, kec):
data = []
rows = driver.find_elements(By.CSS_SELECTOR, 'div:nth-child(2) > table > tbody > tr')
for row in rows:
wilayah = row.find_element(By.CSS_SELECTOR, 'td.text-xs-left.wilayah-name > button').text
persentasi = row.find_element(By.CSS_SELECTOR, 'td.text-xs-left.wilayah-name > span').text
jokowi = row.find_element(By.CSS_SELECTOR, 'td:nth-child(2)').text
prabowo = row.find_element(By.CSS_SELECTOR, 'td:nth-child(3)').text
data.append([prob, kab, kec, wilayah, persentasi, jokowi, prabowo])
return data
data = []
provinsi = driver.find_elements(By.CSS_SELECTOR, 'div:nth-child(1) > table > tbody > tr')
button = provinsi[1].find_element(By.TAG_NAME, 'button')
pro = button.text
wait.until(EC.element_to_be_clickable(button)).click()
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div:nth-child(1) > table > tbody > tr')))
for i in [1,2]:
time.sleep(1)
kabupaten = driver.find_elements(By.CSS_SELECTOR, 'div:nth-child(' + str(i) + ') > table > tbody > tr')
for kab in kabupaten:
time.sleep(1)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div:nth-child(' + str(i) + ') > table > tbody > tr')))
kab_button = kab.find_element(By.TAG_NAME, 'button')
kab_name = kab_button.text
driver.execute_script("arguments[0].scrollIntoView();", kab_button)
driver.execute_script("arguments[0].click();", kab_button)
for i in [1,2]:
time.sleep(1)
kecamatan = driver.find_elements(By.CSS_SELECTOR, 'div:nth-child(' + str(i) + ') > table > tbody > tr')
for kec in kecamatan:
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div:nth-child(' + str(i) + ') > table > tbody > tr')))
kec_button = kec.find_element(By.CSS_SELECTOR, 'td.text-xs-left.wilayah-name > button')
kec_name = kec_button.text
driver.execute_script("arguments[0].scrollIntoView();", kec_button)
driver.execute_script("arguments[0].click();", kec_button)
kelurahan = driver.find_elements(By.CSS_SELECTOR, 'div:nth-child(1) > table > tbody > tr')
time.sleep(1)
left_table = scrape_left_table(pro, kab_name, kec_name)
right_table = scrape_right_table(pro, kab_name, kec_name)
data += left_table + right_table
back = driver.find_element(By.CSS_SELECTOR, '#app > div.sticky-top.bg-white > div > div:nth-child(2) > div > div > div > div:nth-child(5) > div > div > div.vs__actions > button')
driver.execute_script("arguments[0].scrollIntoView();", back)
driver.execute_script("arguments[0].click();", back)
back = driver.find_element(By.CSS_SELECTOR, '#app > div.sticky-top.bg-white > div > div:nth-child(2) > div > div > div > div:nth-child(4) > div > div > div.vs__actions > button')
driver.execute_script("arguments[0].scrollIntoView();", back)
driver.execute_script("arguments[0].click();", back)
在特定迭代之后,即对于provinsi[0]
,在689次迭代之后出现误差;对于provinsi[1]
,在35次迭代之后出现误差。
WebDriverException Traceback (most recent call last)
c:\...\web_scraping.ipynb Cell 4 in ()
23 for kec in kecamatan:
24 wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div:nth-child(' + str(i) + ') > table > tbody > tr')))
---> 26 kec_button = kec.find_element(By.CSS_SELECTOR, 'td.text-xs-left.wilayah-name > button')
27 kec_name = kec_button.text
28 driver.execute_script("arguments[0].scrollIntoView();", kec_button)
WebDriverException: Message: unknown error: unhandled inspector error: {"code":-32000,"message":"No node with given id found"}
2条答案
按热度按时间kyks70gy1#
这似乎是最近的ChromeDriver v113的缺陷:https://bugs.chromium.org/p/chromedriver/issues/detail?id=4440
目前看来,这是最有可能的嫌疑人:
当与之交互的元素被Chromedriver确定为过时时,会发生这种情况
在这种情况下,WebDriver似乎是由于缺陷而抛出了WebDriverException,而不是StaleElementReferenceException(这是C#中的)。
e5nszbig2#
这只会发生在某些DOM元素中,比如
.gif
和来自不同iframe
的警报消息。在检查元素后使用driver.switchTo().defaultContent();
很有帮助。