ÓùÛà19090 ·¢±íÓÚ 2023-2-13 11:11:22

֮ǰһģһÑùµÄ¶¼Õý³£ÔËÐÐÁË

Õâ¸öΪʲô±¨´íѽ£¬¿´²»¶®
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

web = webdriver.Chrome()

web.get("http://lagou.com")

# ÕÒµ½Ä³¸öÔªËØ,µã»÷Ëü

el = web.find_element(By.XPATH, '//*[@id="changeCityBox"]/p/a')

el.click()#µã»÷ʼþ

#time.sleep(1)   #ÓÐʱ,ÒªÈÃä¯ÀÀÆ÷»ºÒ»»á¶ù

#ÕÒµ½ÊäÈë¿ò, => ÊäÈë»Ø³µ/µã»÷ËÑË÷°´Å¥
web.find_element(By.XPATH, '//*[@id="search_input"]').send_keys("python", Keys.ENTER)

#²éÕÒ´æ·ÅÊý¾ÝµÄλÖ᣽øÐÐÊý¾ÝÌáÈ¡
#ÕÒµ½Ò³ÃæÖдæ·ÅÊý¾ÝµÄËùÓеÄdiv
item__10RTO = web.find_elements(By.XPATH, '//*[@id="jobList"]/div/div')
for item in item__10RTO:
    name = item.find_element(By.TAG_NAME, "a").text
    #condition = item__10RTO = web.find_element(By.XPATH, './div/div/div')
    #wages = item.find_element(By.XPATH, './div/div/div/span')
    print(name)

time.sleep(6000)Traceback (most recent call last):
File "C:\Users\misaka\PycharmProjects\pythonProject\ÅÀ³æ\µÚÎåÕÂ\seleniumµÄ¸÷ÖÖ²Ù×÷.py", line 25, in <module>
    name = item.find_element(By.TAG_NAME, "a").text
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\misaka\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 425, in find_element
    return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})["value"]
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\misaka\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 403, in _execute
    return self._parent.execute(command, params)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\misaka\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
File "C:\Users\misaka\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, 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=110.0.5481.78)
Stacktrace:
Backtrace:
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        GetHandleVerifier
        GetHandleVerifier
        GetHandleVerifier
        GetHandleVerifier
        (No symbol)
        (No symbol)
        (No symbol)
        (No symbol)
        BaseThreadInitThunk
        RtlInitializeExceptionChain
        RtlClearBits

isdkz ·¢±íÓÚ 2023-2-13 11:11:23

±¾Ìû×îºóÓÉ isdkz ÓÚ 2023-2-13 11:27 ±à¼­

ÒòΪҳÃ滹ûÓÐÍê³É¼ÓÔسöÀ´£¬ËùÒÔÓà time.sleep ÈÃËüµÈ´ýÒ³ÃæÍêÈ«¼ÓÔØ
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

web = webdriver.Chrome("chromedriver.exe")

web.get("http://lagou.com")

# ÕÒµ½Ä³¸öÔªËØ,µã»÷Ëü

el = web.find_element(By.XPATH, '//*[@id="changeCityBox"]/p/a')

el.click()#µã»÷ʼþ

#time.sleep(1)   #ÓÐʱ,ÒªÈÃä¯ÀÀÆ÷»ºÒ»»á¶ù

#ÕÒµ½ÊäÈë¿ò, => ÊäÈë»Ø³µ/µã»÷ËÑË÷°´Å¥
web.find_element(By.XPATH, '//*[@id="search_input"]').send_keys("python", Keys.ENTER)

#²éÕÒ´æ·ÅÊý¾ÝµÄλÖ᣽øÐÐÊý¾ÝÌáÈ¡
#ÕÒµ½Ò³ÃæÖдæ·ÅÊý¾ÝµÄËùÓеÄdiv
time.sleep(0.1)                                                                                                 # ¼ÓÁËÕâÐÐ
item__10RTO = web.find_elements(By.XPATH, '//*[@id="jobList"]/div/div')
for item in item__10RTO:
    name = item.find_element(By.TAG_NAME, "a").text
    #condition = item__10RTO = web.find_element(By.XPATH, './div/div/div')
    #wages = item.find_element(By.XPATH, './div/div/div/span')
    print(name)

time.sleep(6000)
Ò³: [1]
²é¿´ÍêÕû°æ±¾: ֮ǰһģһÑùµÄ¶¼Õý³£ÔËÐÐÁË