Selenium Webdirver Error 가 해결이 되지 않습니다.

hyemin1826의 이미지

import telegram
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from pathos.multiprocessing import ProcessingPool as Pool
from datetime import datetime
import schedule
import time
import sys
from selenium.webdriver.support.ui import WebDriverWait

if __name__=='__main__':
options = webdriver.ChromeOptions()
options.add_argument("--headless") # Background(CLI) 동작 사용
options.add_argument("--no-sandbox")
options.add_argument('--disable-dev-shm-usage')
driver=webdriver.Chrome("C:\ChromeDriver\chromedriver.exe", options=options)

latest_num=0

token='1044075845:AAHB0zc0Nt9qY1HtIFTODpS3DP4Aay1qRys'
bot=telegram.Bot(token=token)

def post_mess(post_message, post_list):
for i in range(len(post_list)):
post=post_list[i] #i번째 post
print(post.text)
upload_date=post.find_element_by_xpath("//*[@id='content']/div[3]/div[2]/table/tbody/tr["+str(i+1)+"]/td[5]").text
print("upload_date is "+upload_date)
upload_date=datetime.strptime(upload_date, "%Y.%m.%d")
print(post.find_element_by_xpath("//*[@id='content']/div[3]/div[2]/table/tbody/tr["+str(i+1)+"]/td[2]"))

if (datetime.strptime("2020.05.04", "%Y.%m.%d")==upload_date):
title=post.find_element_by_xpath("//*[@id='content']/div[3]/div[2]/table/tbody/tr["+str(i+1)+"]/td[2]").text
print(title+"\n")
post.find_element_by_xpath("//td[2]/a").click() #제목 클릭

url=driver.current_url
print(url)
post_message+=("["+title+"]\n")
post_message+=("\t"+url+"\n\n")

driver.back()

time.sleep(3)

def crawling(driver, bot):
driver.get("http://www.bizinfo.go.kr/see/seea/selectSEEA100.do")

buiz_form_list=driver.find_elements_by_xpath("//*[@id='seea100VO']/fieldset/div[3]/div[2]/div[4]/ul//li")
for index, buiz_form in enumerate(buiz_form_list):
if index>=4:#1인기업부터
buiz_form.click() #클릭

driver.find_element_by_xpath("//*[@id='seea100VO']/fieldset/div[3]/div[3]/ul/li[2]/a").click() #검색클릭
post_list=driver.find_elements_by_xpath("//*[@id='content']/div[3]/div[2]/table/tbody//tr")

post_message=""

post_message=post_mess(post_message, post_list)

schedule.every(2000).seconds.do(crawling(driver,bot))

while(1):
schedule.run_pending()
time.sleep(2)

(중간에 질문과 상관 없는 코드는 삭제했습니다.)
이렇게 코드를 적었을 때

Traceback (most recent call last):
File "C:\Python\Python 3.8\Crawling\telegrambot_crawling_few.py", line 135, in <module>
schedule.every(2000).seconds.do(crawling(driver,bot))
File "C:\Python\Python 3.8\Crawling\telegrambot_crawling_few.py", line 99, in crawling
post_message=post_mess(post_message, post_list)
File "C:\Python\Python 3.8\Crawling\telegrambot_crawling_few.py", line 27, in post_mess
print(post.text)
File "C:\Python\Python 3.8\lib\site-packages\selenium\webdriver\remote\webelement.py", line 76, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "C:\Python\Python 3.8\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Python\Python 3.8\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python\Python 3.8\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: headless chrome=81.0.4044.129)

이라고 오류가 뜹니다. 저는 이 에러가 page가 바뀌면서 새 driver가 오래된 element를 찾지 못해 발생하는 에러라고 알고 있는데요, driver.back()을 해도, time.sleep()을 해도 오류가 해결되지 않습니다...
도저히 알 수 없어 이렇게 질문 올립니다. 도와주시면 감사하겠습니다 ㅠㅠ

※사이트를 처음 이용하다 보니 코드를 어떻게 올려야 할지 모르겠군요. ㅎ..ㅠ

파이썬3의 이미지

중간중간에
각 변수마다 print 로 일일이 값을 확인해보세요.
각 변수마다 의도한 값이 들어갔나 아닌가를 판별합니다.

그럼 쉽게 해결책을 찾을 수 있을거에요.

[우분투 18.04 파여폭스 나비에서 적었어요~]

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.