selenium python在自动抓取期间发生TimeOutException

9rygscc1  于 2022-12-29  发布在  Python
关注(0)|答案(1)|浏览(131)

TimeOutException是可以避免的吗?或者有什么方法可以修复这个错误吗?问题是,它随机出现在任何行中,所以我无法指定哪些行出现TimeOutException。至于下面的代码,它出现在detail_topic上。使用诸如单击之类的操作时,经常发生这个错误

#loop how many pages
for i in range(0, 10):
    #loop how many topic
    total = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="topic-list"]/card-topic')))
    #loop every topic
    for i in range(1, len(total)):
        
        time.sleep(0.5)
        topic = driver.find_element(By.XPATH, '//*[@id="topic-list"]/card-topic[{}]'.format(i))
        time.sleep(0.5)
        shadow1 = driver.execute_script("return arguments[0].shadowRoot", topic)
        time.sleep(0.5)
        shadow1.find_element(By.CSS_SELECTOR, 'a').send_keys(u'\ue007')
    
        detail_topic = WebDriverWait(driver, 100).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'detail-topic')))
        shadow2 = driver.execute_script("return arguments[0].shadowRoot", detail_topic)
        title = shadow2.find_element(By.CSS_SELECTOR, '.h2').text
        title_data3ab.append(title)

        time.sleep(0.5)
        driver.back()
    
    pagination = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f'//paginate-button')))
    shadow_pagination = driver.execute_script("return arguments[0].shadowRoot", pagination)
    next_button = shadow_pagination.find_element(By.CSS_SELECTOR, 'a.page-next')
    actions = ActionChains(driver)
    time.sleep(0.5)
    actions.move_to_element(next_button)
    time.sleep(0.5)
    actions.click(next_button).perform()

我还没有尝试&除了因为错误发生在随机行,所以我需要找出哪一个是主要问题,任何帮助将不胜感激谢谢

izj3ouym

izj3ouym1#

无论如何,我通过在我的webdriver chrome_options.add_extension('yourextensionchoice.crx') #had to be in the same path上添加adblock来解决这个问题

相关问题