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

2019-01-30 19:58:28 +08:00
 spotfg
# 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()
3018 次点击
所在节点    Python
4 条回复
songn80
2019-01-31 01:05:09 +08:00
不知道是不是手机的原因,看不到缩进啊
猜测是 acctions.click 这句有问题吧
需要清一下之前的 action 队列的
不久前遇到过这个问题
action.reset_actions()一下
wc110302
2019-01-31 10:01:43 +08:00
不是的
spotfg
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
2019-02-02 22:58:39 +08:00
对标签用.click()是实时执行的
而 action_chain 是先构建一个队列,执行 actions.perform 的时候执行队列
如果队列不清空的话队列会员一直累积 每次循环都会向队列新增动作且保留之前的动作

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/531967

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX