推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
spotfg
V2EX  ›  Python

selenium 模块,模拟点击在 for 循环下,只点击一次?

  •  
  •   spotfg · Jan 30, 2019 · 4027 views
    This topic created in 2799 days ago, the information mentioned may be changed or developed.
    # coding=utf-8

    from lxml import etree

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver import ActionChains
    import time

    driver = webdriver.Chrome()
    driver.get('https://www.manhuatai.com/fengqicanglan/115hs.html')

    menu = driver.find_element_by_css_selector(".mh_nextpage")
    actions = ActionChains(driver)

    for i in range(3):
    # 智能等待
    try:
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "comiclist")))
    except NoSuchElementException:
    print('找不到元素异常')
    # 获取图片链接
    res = driver.page_source
    html = etree.HTML(res)
    pictrue_url = html.xpath('//div[@class="mh_comicpic"]/img/@src')[0]
    print(pictrue_url)

    # 在“下一页”位置点击鼠标左键
    actions.click(menu)
    actions.perform()

    driver.close()
    4 replies  •  2019-02-02 22:58:39 +08:00
    songn80
        1
    songn80  
       Jan 31, 2019 via Android
    不知道是不是手机的原因,看不到缩进啊
    猜测是 acctions.click 这句有问题吧
    需要清一下之前的 action 队列的
    不久前遇到过这个问题
    action.reset_actions()一下
    wc110302
        2
    wc110302  
       Jan 31, 2019
    不是的
    spotfg
        3
    spotfg  
    OP
       Feb 2, 2019
    @songn80
    现在还是没搞明白,为啥,但是我改了下代码,将下面的这几句都删了
    =========
    menu = driver.find_element_by_css_selector(".mh_nextpage")
    actions = ActionChains(driver)
    # 在“下一页”位置点击鼠标左键
    actions.click(menu)
    actions.perform()
    =========
    在 for 循环最下面加了里最下面
    driver.find_element_by_css_selector(".mh_nextpage").click()
    songn80
        4
    songn80  
       Feb 2, 2019 via Android   ❤️ 1
    对标签用.click()是实时执行的
    而 action_chain 是先构建一个队列,执行 actions.perform 的时候执行队列
    如果队列不清空的话队列会员一直累积 每次循环都会向队列新增动作且保留之前的动作
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3197 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 00:35 · PVG 08:35 · LAX 17:35 · JFK 20:35
    ♥ Do have faith in what you're doing.