等待后,过时的元素无法解决,python

我正在尝试进入此页面上“课程”类的每个链接

<a name="hrvatski-jezik" href="/pregled/predmet/29812177240/1971997880"><div class="course">
    Hrvatski jezik   <br>
        <span class="course-info">Tamara Čer</span>
    </div>
</a>
<a name="likovna-kultura" href="/pregled/predmet/29812176230/1971998890">
    <div class="course">Likovna kultura   <br>
        <span class="course-info">Mia  Marušić</span>
    </div>
</a>
<a name="glazbena-kultura" href="/pregled/predmet/29812175220/1971999900">
    <div class="course">Glazbena kultura   <br>
        <span class="course-info">Danijel Služek</span>
    </div>
</a>
<a name="engleski-jezik" href="/pregled/predmet/29820696590/1972511970">
    <div class="course">Engleski jezik   <br>
        <span class="course-info">Nevena Genčić</span>
    </div>
</a>    
<a name="matematika" href="/pregled/predmet/29812174210/1972000910">
    <div class="course">Matematika   <br>
        <span class="course-info">Ivan Tomljanović</span>
    </div></a>
<a name="biologija" href="/pregled/predmet/29812173200/1972001920">
    <div class="course">Biologija   <br>
        <span class="course-info">Antonija Milić</span>
    </div>
</a>
<a name="kemija" href="/pregled/predmet/29812172190/1972002930">
    <div class="course">Kemija   <br>
        <span class="course-info">Antonija Milić</span>
    </div>
</a>
<a name="fizika" href="/pregled/predmet/29812171180/1972003940">
    <div class="course">Fizika   <br>
        <span class="course-info">Ivan Kunac</span>
    </div>
</a>
<a name="povijest" href="/pregled/predmet/29812170170/1972004950">
    <div class="course">Povijest   <br>
        <span class="course-info">Lovorka Krajnović Tot</span>
    </div>
</a>
<a name="geografija" href="/pregled/predmet/29812169160/1972005960">
    <div class="course">Geografija   <br>
        <span class="course-info">Sunčica Podolski <strong> (na zamjeni)</strong>, Oliver Timarac</span>
    </div>
</a>
<a name="tehnicka-kultura" href="/pregled/predmet/29812168150/1972006970">
    <div class="course">Tehnička kultura   <br>
        <span class="course-info">Ivan Dorotek</span>
    </div>
</a>
<a name="tjelesna-i-zdravstvena-kultura" href="/pregled/predmet/29812167140/1972007980">
    <div class="course">Tjelesna i zdravstvena kultura   <br>
        <span class="course-info">Davor  Marković, Tomislav Ruskaj</span>
    </div>
</a>
<a name="informatika" href="/pregled/predmet/29821462170/1972568530">
    <div class="course">Informatika (izborni)  <br>
        <span class="course-info">Blaženka Knežević</span>
    </div>
</a>
<a name="njemacki-jezik" href="/pregled/predmet/32658461270/1972646300"><div class="course">Njemački jezik (izborni)  <br>
    <span class="course-info">Zdravka Marković Boto</span>
</div>
</a>
<a name="rusinski-jezik-i-kultura" href="/pregled/predmet/32658491570/1972675590">
    <div class="course">Rusinski jezik i kultura (izborni)  <br>
        <span class="course-info">Natalija Hnatko, Ilona Hrecešin</span>
    </div>
</a>
<a name="sat-razrednika" href="/pregled/predmet/32322897860/2140793120">
    <div class="course">Sat razrednika   <br>
        <span class="course-info">Blaženka Knežević</span>
    </div>
</a>
<a name="izvannastavne-aktivnosti" href="/pregled/predmet/34285616720/2324344460">
    <div class="course">Izvannastavne aktivnosti (izvannastavna aktivnost)  <br>
        <span class="course-info">Nevena Genčić, Ivan Kunac, Davor  Marković, Josip Matezović, Antonija Milić, Tomislav Ruskaj, Danijel Služek</span>
    </div>
</a>

` 我希望代码能进入每个链接,然后返回并重复。 它进入try块一次,然后进入except块16次。

For every except it gives StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

我的代码:

def get_subject():

    subjects = driver.find_elements_by_xpath("//div[@class='course']")

    for subject in subjects: 
        actions = ActionChains(driver)
        actions.move_to_element(subject)    

        try:
            actions.click()
            actions.perform()

            driver.back()
            print("try")

            time.sleep(3)

        except Exception as e:
            subjects = driver.find_elements_by_xpath("//div[@class='course']")
            print("except")
            print(e)

I know this is a very common problem. I tried implicit and explicit waits, I still got the same error. I tried visibility_of_element_located, presence_of_element_located, staleness_of, I tried defining "subjects" again.

非常感谢您的帮助,我一直在寻找解决方案。

评论
  • 啊呸!
    啊呸! 回复

    driver.back()函数不能保证正常工作。而是尝试使用此driver.execute_script(“ window.history.go(-1)”)

  • Stanf
    Stanf 回复

    actions.move_to_element(subject) is the culprit here.

    all the element references in the subjects will be refreshed when you click on the subject in your try block, that's why you are getting the StateleElementException. Try changing the code as shown below.

    def get_subject():
    
        subjects = len(driver.find_elements_by_xpath("//div[@class='course']"))
    
        for counter in range(subjects):
            subject = driver.find_elements_by_xpath("(//div[@class='course'])[" + str(counter) + "]") 
            actions = ActionChains(driver)
            actions.move_to_element(subject)    
    
            try:
                actions.click()
                actions.perform()
    
                driver.back()
                print("try")
    
                time.sleep(3)
    
            except Exception as e:
                print("except")
                print(e)
    
  • trerum
    trerum 回复

    I would suggest capture all links first and then iterate.

    alllinks=[link.get_attribute('href') for link in driver.find_elements_by_css_selector("a[href^='/pregled/predmet']")]
    for link in alllinks:
        driver.get(link)
        #Perform your operation
    

    如果要继续执行代码,只需重新分配元素即可。由于您正在使用driver.back(),因此页面会刷新。

    def get_subject():
    
        subjects = driver.find_elements_by_xpath("//div[@class='course']")
    
        for subject in range(len(subjects)):
            subjects = driver.find_elements_by_xpath("//div[@class='course']")
            actions = ActionChains(driver)
            actions.move_to_element(subjects[subject])    
    
            try:
                actions.click()
                actions.perform()
    
                driver.back()
                print("try")
    
                time.sleep(3)
    
            except Exception as e:
                subjects = driver.find_elements_by_xpath("//div[@class='course']")
                print("except")
                print(e)