在上一篇文章《Python|PyAutoGUI实现自动点赞收藏 》中写过利用 PyAutoGUI 实现网页自动化点赞+收藏的功能,实测发现依靠屏幕坐标系完成鼠标操作的弊端(因为布局原因导致图标位置变化时,不能很好的处理),IT人当然不能接受这种瑕疵😂😂😂
今天用另一个 python 库实现全天候、全地域的精准点击😁😁😁
思路
和上一篇文章一样,也是基于网页实现循环打开未读文章,之后进行点赞+收藏操作。不同的是今天使用的 selenium 库在元素定位时是基于网页html的格式,可以通过元素 id 、 name 、 class 、 css 甚至 xpath 实现精准定位。在网页的处理上比 PyAutoGUI 有更高的稳定性。
测试效果
源码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 from selenium import webdriverimport timefrom os import pathfrom selenium.webdriver.common.by import By driver = webdriver.Edge('C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe' )def login (): driver.get('https://XXXX/' ) driver.maximize_window() time.sleep(25 ) print ('----------登录成功----------' ) if driver.find_element(By.XPATH,'//img[@alt="资讯"]' ): print ('----------打开资讯----------' ) driver.find_element(By.XPATH,'//img[@alt="资讯"]' ).click() time.sleep(1 ) print ('----------打开推荐----------' ) driver.find_element(By.XPATH,'//div[text()="推荐"]' ).click() time.sleep(1 )def auto_click (num ): i = 0 while i<num: driver.find_element(By.XPATH,'//span[text()="热度优先"]' ).click() time.sleep(0.5 ) driver.find_element(By.XPATH,'//span[text()="时间优先"]' ).click() time.sleep(0.5 ) driver.find_element(By.XPATH,'//div [@class="home-main-partyNews" and @style="margin-top: 0px;"]//div [@class="dynamic-left-title"]' ).click() time.sleep(1 ) window = driver.current_window_handle all_handles = driver.window_handles driver.switch_to.window(all_handles[-1 ]) try : driver.find_element(By.XPATH,'//div[@class="icon"]/span[@class="pointer"]/i[@class="iconfont icon-shoucanghuise"]' ).click() print ('第{}篇文章收藏成功' .format (i+1 )) except : try : driver.find_element(By.XPATH,'//div[@style="display: block;"]/span[@class="header-icon"]/i[@class="iconfont icon-weishoucang"]' ).click() print ('第{}篇文章收藏成功' .format (i+1 )) except : print (driver.current_url) pass time.sleep(0.2 ) try : driver.find_element(By.XPATH,'//div[@class="icon"]/span[@class="pointer"]/i[@class="iconfont icon-dianzanhuise"]' ).click() print ('第{}篇文章点赞成功' .format (i+1 )) except : try : driver.find_element(By.XPATH,'//div[@style="display: block;"]/span[@class="header-icon"]/i[@class="iconfont icon-dianzanhuise"]' ).click() print ('第{}篇文章点赞成功' .format (i+1 )) except : print (driver.current_url) pass time.sleep(1 ) driver.close() driver.switch_to.window(window) time.sleep(0.5 ) i +=1 print ('----------执行完毕----------' )if __name__ == "__main__" : login() auto_click(10 )
总结
selenium 同样是一款 Web 自动化测试工具,但基本都被用来爬虫了。果真 python 的一切自动化测试工具的归宿都是爬虫😂😂😂
selenium 还是很强大的,在 web 页面解析、浏览器控制、鼠键处理上都是很灵活的,可以处理一些相对复杂的场景。并且selenium 支持 IE 、 chrome 、 edge 等常用浏览器,使用时需要借助 webdriver 插件完成。( webdriver 插件最好和本地计算机对应浏览器的版本保持一致)
需要提示的是,selenium的4.*版本语法做了更新,和3.*差异较大,使用时需留意官方文档。
附:selenium库基本用法整理
模块导入
1 2 3 4 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysimport time
调用webdriver
1 driver = webdriver.Edge("C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe" )
使用get方法加载url界面
1 driver.get(r'https://www.baidu.com' )
断言是否存在
1 assert '百度一下,你就知道' in driver.title
通过 find _ element 方法查找元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 element = driver.find_element(By.ID, "passwd-id" ) element = driver.find_element(By.NAME, "passwd" ) element = driver.find_element(By.CLASS_NAME, "text-a" ) element = driver.find_element(By.XPATH, "//input[@id='passwd-id']" ) element = driver.find_element(By.CSS_SELECTOR, "input#passwd-id" )
键盘输入
1 2 3 element.clear() element.send_keys("pycon" ) element.send_keys(Keys.RETURN)
处理下拉选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysimport time driver = webdriver.Edge("C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe" ) driver.get(r'https://www.sojson.com/time/jsq.html' ) element = driver.find_element(By.ID, "month3" ) all_options = element.find_elements(By.TAG_NAME, "option" )for option in all_options: print ("Value is: {}" .format (option.get_attribute("value" ))) if option.get_attribute("value" )==10 : option.click() time.sleep(10 )
处理选择
1 2 3 4 5 6 7 8 from selenium.webdriver.support.ui import Select select = Select(driver.find_element(By.NAME, 'name' )) select.select_by_index(index) select.select_by_visible_text("text" ) select.select_by_value(value) select = Select(driver.find_element(By.ID, 'id' )) select.deselect_all()
处理拖放
1 2 3 4 5 6 7 element = driver.find_element(By.NAME, "source" ) target = driver.find_element(By.NAME, "target" )from selenium.webdriver import ActionChains action_chains = ActionChains(driver) action_chains.drag_and_drop(element, target).perform()
实现窗口间的切换
1 driver.switch_to_window("windowName" )
触发弹窗后可以通过这种方式获取警报
1 alert = driver.switch_to.alert
处理浏览器(当前网页)的前进/后退
1 2 driver.forward() driver.back()
参考文档
Selenium with Python