안녕하세요 selenium 질문드립니다.

bedals의 이미지

# -*- coding: utf-8 -*-
"""
Created on Tue Apr 23 22:18:31 2019
 
@author: Administrator
 
"""
from selenium import webdriver
import time
import random
 
 
 
# 구분값
def serch_names(item):
    babo = driver.find_element_by_id("query")
    babo.send_keys(item)
    babo = driver.find_element_by_id("search_btn")
    babo.click()
    return item
for x in range(5):
# 검색창 검색
    serch_num = random.randrange(0,3)
    time_serch = random.randrange(0,3)
    driver = webdriver.Chrome('C:\chromedriver.exe')
    driver.get('http://www.naver.com')
    driver.implicitly_wait(5)
 
 
    if serch_num == 0:
        serch_names('박지성')
        babo = driver.find_element_by_id("nx_query")
        time.sleep(time_serch)
        babo.clear()
        babo.send_keys('탈모샴푸')
        babo.find_element_by_xpath('//*[@id="nx_search_form"]/fieldset/button').click()
        babo.clear()
        babo.find_element_by_xpath('//*[@id="_shopping_guide_view"]/div[3]/a').click()
        driver.switch_to.window(driver.window_handles[-1])
        babo.find_element_by_xpath('//*[@id="_filter"]/div[3]/ul/li[4]/a/span').click()
        driver.switch_to.window(driver.window_handles[-1])
        babo.find_element_by_xpath('//*[@id="_search_list"]/div[1]/ul/li[1]/div[2]/a').click()
        driver.switch_to.window(driver.window_handles[-1])
        babo.find_element_by_xpath('//*[@id="_mainSummaryPrice"]/div[1]/a').click()
        driver.execute_script("window.scrollTo(0,900)") 
        driver.close()
    elif serch_num ==1:
        serch_names('손흥민')
        babo = driver.find_element_by_id("nx_query")
        time.sleep(time_serch)
        babo.clear()
        babo.send_keys('건강식품')
        driver.close()
    elif serch_num ==2:
        serch_names('설기현')
        babo = driver.find_element_by_id("nx_query")
        time.sleep(time_serch)
        babo.clear()
        babo.send_keys('뷰티윈도우')
        drive.clear()
 
 
실행하면 박지성 부분에서 
StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 6.1.7601 SP1 x86_64)
 
다음과 같은 오류가 발생하는데요 이유를 알아보려고 구글링 해본 결과 페이지가 로딩이 안되서 그렇다고 하는데
time.sleep()으로 딜레이를 줘도 계속 오류가 발생합니다.
힌트좀 부탁드리겠습니다.
raymundo의 이미지

selenium을 잘 알지는 못하지만 해보니 창의 내용이 새로 고쳐져서 기존 객체를 계속 쓸 수 없나봅니다.

매번 다시 babo 변수를 갱신하세요.

babo.find....click() # 버튼 클릭해서 검색 결과가 새로 떴으면
 
babo = driver.find_element_by_id("nx_query") # 입력필드를 다시 찾음
babo.clear()
...

좋은 하루 되세요!

bedals의 이미지

답변 너무감사합니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.