V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-01-30 19:58:28 +08:00 · 3001 次点击
    这是一个创建于 1884 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # 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 条回复    2019-02-02 22:58:39 +08:00
    songn80
        1
    songn80  
       2019-01-31 01:05:09 +08:00 via Android
    不知道是不是手机的原因,看不到缩进啊
    猜测是 acctions.click 这句有问题吧
    需要清一下之前的 action 队列的
    不久前遇到过这个问题
    action.reset_actions()一下
    wc110302
        2
    wc110302  
       2019-01-31 10:01:43 +08:00
    不是的
    spotfg
        3
    spotfg  
    OP
       2019-02-02 21:48:18 +08:00
    @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  
       2019-02-02 22:58:39 +08:00 via Android   ❤️ 1
    对标签用.click()是实时执行的
    而 action_chain 是先构建一个队列,执行 actions.perform 的时候执行队列
    如果队列不清空的话队列会员一直累积 每次循环都会向队列新增动作且保留之前的动作
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3222 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 14:23 · PVG 22:23 · LAX 07:23 · JFK 10:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.