使用Selenium+Python遇元素不可点击问题:动态页面爬取异常
动态加载网站爬取困境:元素点击拦截与长加载元素适配失败
爬取某动态加载网站时,部分元素加载耗时长达1-2分钟,使用time.sleep()无法灵活适配加载时长,导致程序提前执行触发报错:
Message: element click intercepted: Element is not clickable with Selenium and Python
已尝试各类WebDriverWait解决方案,但均未生效,当前代码如下:
import os import platform from selenium.webdriver.common.by import By if platform.system() == "Windows": try: import undetected_chromedriver from parsel import Selector from selenium import webdriver except ImportError: os.system('python -m pip install parsel') os.system('python -m pip install selenium') os.system('python -m pip install undetected_chromedriver') else: try: from parsel import Selector from selenium import webdriver import undetected_chromedriver except ImportError: os.system('python3 -m pip install parsel') os.system('python3 -m pip install selenium') os.system('python3 -m pip install undetected_chromedriver') import undetected_chromedriver as uc import csv import os from parsel import Selector import time from selenium import webdriver filename = "cadastre" if __name__ == '__main__': driver = uc.Chrome() time.sleep(5) driver.get("https://kais.cadastre.bg/bg/Map") input("Ready (Y/N) : ") if filename+'.csv' not in os.listdir(os.getcwd()): with open(filename+".csv","a",newline="",encoding="utf-8") as f: writer = csv.writer(f) while True: print("Waiting for 3 sec") time.sleep(3) response = Selector(text=driver.page_source) uids = response.xpath('.//*[@id="resultsList"]//@data-uid').extract() print(uids) for uid in uids: print("Clicking : "+str(uid)) driver.find_element(by=By.XPATH,value='.//*[@data-uid="'+str(uid)+'"]/a').click() time.sleep(1) for uid in uids: sel = Selector(text=driver.page_source).xpath('.//*[@data-uid="'+str(uid)+'"]') textfile = ','.join([i.strip() for i in sel.xpath('.//*[@class="object-properties"]/p//text()').extract() if i.strip()]) if textfile: with open(filename+".csv","a",newline="",encoding="utf-8") as f: writer = csv.writer(f) writer.writerow([textfile]) print([textfile]) current_page = Selector(text=driver.page_source).xpath('.//*[@id="resultsList_pager"]//*[@class="k-link k-pager-nav"]/text()').extract_first() if Selector(text=driver.page_source).xpath('.//*[@data-page="'+str(int(current_page)+1)+'"]'): driver.execute_script("document.getElementsByClassName('k-link k-pager-nav')[8].click();") else: break driver.close()
内容的提问来源于stack exchange,提问作者Az Pak Az
相关产品推荐
相关产品推荐

