第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

即使使用 WebDriverWait 也是陳舊的元素

即使使用 WebDriverWait 也是陳舊的元素

我不明白為什么我會(huì)收到此錯(cuò)誤:raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of <a id="u153-popover-trigger--3926" class="udlite-custom-focus-visible browse-course-card--link--3KIkQ" href="/course/kafka-streams-real-time-stream-processing-master-class/"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed我使用 WebDriverWait 兩次來(lái)檢查是否加載了新頁(yè)面:如果新頁(yè)面的分頁(yè)鏈接發(fā)生相應(yīng)變化如果新頁(yè)面的課程列表div元素被加載  from selenium import webdriver    from selenium.webdriver.common.keys import Keys    from selenium.webdriver.firefox.options import Options    from selenium.webdriver.common.by import By    from selenium.webdriver.support.ui import WebDriverWait    from selenium.webdriver.support import expected_conditions as EC    from selenium.common.exceptions import TimeoutException        def waitForLoad(inputXPath):         Wait = WebDriverWait(driver, 10)        Wait.until(EC.presence_of_element_located((By.XPATH, inputXPath)))        options = Options()    options.headless = True    driver = webdriver.Firefox(options=options, service_log_path='NUL')        driver.get("https://www.udemy.com/courses/development/?sort=highest-rated")        courses = []    f = open("0udemy.txt","a", encoding="utf-8")    page = 1        try:        waitForLoad("//div[@class='filter-panel--paginated-course-list--2F0x1']")    except TimeoutException as e:        print("timeout!")        while True:        ## I also tried that :         #courses = driver.find_elements_by_xpath("//div[@class='course-list--container--3zXPS']//a[contains(@class, 'browse-course-card--link--3KIkQ')]")        #for i in courses:        #    f.write(f"{i.get_attribute('href')}\n")我在第 20 頁(yè)和第 80 頁(yè)之間收到陳舊錯(cuò)誤。我的世界紀(jì)錄是第 78 頁(yè)。
查看完整描述

1 回答

?
富國(guó)滬深

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊

我一次只瀏覽了 1 頁(yè)并打印出來(lái)(您可以使用 f.write)。您需要添加 time.sleep() 以便 Selenium 不會(huì)崩潰。這可以永遠(yuǎn)持續(xù)下去,直到頁(yè)面用完為止。或者如果指定 if page==n: 則中斷。它甚至可以經(jīng)歷硒的生命周期。


while True:

    try:

        courses=WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='course-list--container--3zXPS']//a[contains(@class, 'browse-course-card--link--3KIkQ')]")))

        for course in courses:

            print(course.get_attribute('href')+"\n")

        driver.find_elements_by_xpath("//a[@class='udlite-btn udlite-btn-small udlite-btn-secondary udlite-heading-sm udlite-btn-icon udlite-btn-icon-small udlite-btn-icon-round pagination--next--5NrLo']")[0].click()

        page=page+1

        time.sleep(5)

    except:

        url=driver.current_url

        driver.close()

        driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)

        driver.get(url)


查看完整回答
反對(duì) 回復(fù) 2023-10-11
  • 1 回答
  • 0 關(guān)注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)