Question about Python selenium crawling error

Asked 2 years ago, Updated 2 years ago, 36 views

What I wanted to make was to go into the site posting and scratch my writing. Some of the current errors have not yet been scratched, and an error has occurred while trying to create a part that uses the chrome return function to scratch and return to the original bulletin board address

Code is

listlink = 'Board link'
driver.get(listlink)
#Save list contents
allList = driver.find_elements_by_css_selector(
    "#kboard-default-list > div.kboard-list > table > tbody > tr")
#Access the contents of the list (posts within the current page) respectively
for item in allList: 
    postNum = item.find_element_by_tag_name("td").text #The appropriate post number
    postName = item.find_element_by_tag_name("div").text #Name of the post
    if(postName != ''):
        link = item.find_element_by_tag_name("a").get_attribute("href") #PostingAddress
        time.sleep(2)
        driver.get(link) #Go to the appropriate post
        """ "The part to scratch"
        time.sleep(2)
        driver.back() #Scratch and return to bulletin board
    time.sleep(2)

Error content is

Traceback (most recent call last):
  File "D:\downfile\downtest.py", line 38, in <module>
    postName = item.find_element_by_tag_name("div").text
  File "D:\ProgramData\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 305, in find_element_by_tag_name
    return self.find_element(by=By.TAG_NAME, value=name)
  File "D:\ProgramData\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 658, in find_element
    return self._execute(Command.FIND_CHILD_ELEMENT,
  File "D:\ProgramData\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "D:\ProgramData\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "D:\ProgramData\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=90.0.4430.93)

It's like this The element is not attached to the page document It's because the repetition went over before the page was loaded, time.It's better if you add sleep() I'm still getting errors.

python selenium

2022-09-20 16:31

1 Answers

I think you're using the wrong slip.

Do the following:

listlink = 'Board Link'
driver.get(listlink)

time.sleep(10)

#Save list contents
allList = driver.find_elements_by_css_selector(
    "#kboard-default-list > div.kboard-list > table > tbody > tr")
#Access the contents of the list (posts within the current page) respectively
for item in allList: 
    postNum = item.find_element_by_tag_name("td").text #The appropriate post number
    postName = item.find_element_by_tag_name("div").text #Name of the post
    if(postName != ''):
        link = item.find_element_by_tag_name("a").get_attribute("href") #PostingAddress
        time.sleep(2)
        driver.get(link) #Go to the appropriate post
        """ "The part to scratch"
        #time.sleep(2)
        driver.back() #Scratch and return to bulletin board
   # # time.sleep(2)


2022-09-20 16:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.