weixin_43383962 2023-06-15 09:52 采纳率: 100%
浏览 21
已结题

selenium如何实现遍历点击

#selenium如何实现遍历点击啊?

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
from selenium import webdriver
web =webdriver.Edge()
web.get('https://bk.wzxsjw.com/electronicbook')
time.sleep(3)
utllist1 =web.find_elements(By.CLASS_NAME,'coverDiv___rhKSe')
print(utllist1)
for element in utllist1:
    #print(element)
    element.click()
    time.sleep(2)
    returen=web.find_element(By.XPATH, '//*[@id="root"]/div/div/div[1]/div[2]/div[1]/img')
    web.back()
    time.sleep(2)#返回后,出现错误,不再点击?这是什么原因
‘’‘
Traceback (most recent call last):
  File "D:\python笔记\云展网下载.py", line 15, in <module>
    element.click()
  File "C:\Users\liuji\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 94, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\liuji\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\liuji\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\liuji\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: stale element not found
  (Session info: MicrosoftEdge=114.0.1823.43)
Stacktrace:
Backtrace:
    GetHandleVerifier [0x00007FF77C83B582+64226]
    Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF77C7CBB42+770978]
  • 写回答

2条回答 默认 最新

  • weixin_43383962 2023-06-15 10:46
    关注

    我自己想出的办法
    成功实现,下面代码

    from selenium.webdriver.support.ui import WebDriverWait
    
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    import time
    from selenium import webdriver
    web =webdriver.Edge()
    web.get('https://bk.wzxsjw.com/electronicbook')
    time.sleep(3)
    
    utllist1 =web.find_elements(By.CLASS_NAME,'coverDiv___rhKSe')
    
    
    
    time.sleep(2)
    for i in range(0,74):
        #print(element)
    
        utllist1[i].click()
        time.sleep(2)
        returen=web.find_element(By.XPATH, '//*[@id="root"]/div/div/div[1]/div[2]/div[1]/img')
        returen.click()
        time.sleep(2)
        utllist1 = web.find_elements(By.CLASS_NAME, 'coverDiv___rhKSe')
        time.sleep(2)
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月23日
  • 已采纳回答 6月15日
  • 创建了问题 6月15日

悬赏问题

  • ¥15 vue请求不到数据,返回状态200,数据为html
  • ¥15 访问url时不会自动调用其 Servlet的doGet()
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找
  • ¥15 MATLAB解决问题
  • ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享