python模拟浏览器selenium

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python模拟浏览器selenium

一事无成~   2022-05-28 我要评论

首先下载selenium模块,pip install selenium,下载一个浏览器驱动程序(我这里使用谷歌)。

#导入
#注意python各版本find_element()方法的变化(python3.10)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
# 创建一个模拟浏览器对象,然后通过对象去操作浏览器s=Service("chromedriver.exe")browser=webdriver.Chrome(service=s)

QQ空间默认登录是使用二维码登录,我们要使用账号密码登录注意QQ空间登录框在一个iframe标签里:定位该框架

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LmF5oul5LiN6L-H5oCd5b-1772e,size_20,color_FFFFFF,t_70,g_se,x_16

browser.get('https://qzone.qq.com/')
browser.maximize_window()time.sleep(2)
browser.switch_to.frame('login_frame')
a_tag = browser.find_element(By.ID,"switcher_plogin")
a_tag.click()

 接下来就是输入账号,密码,点击登录

userName_tag = browser.find_element(By.ID,'u')
password_tag =browser.find_element(By.ID,'p')
time.sleep(1)
userName_tag.send_keys('这里是QQ号')
time.sleep(1)
password_tag.send_keys('这里是密码')
time.sleep(1)
btn = browser.find_element(By.ID,'login_button')
btn.click()

 目前实现的效果图

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LmF5oul5LiN6L-H5oCd5b-1772e,size_20,color_FFFFFF,t_70,g_se,x_16

接下来实现的是,进入上边导航栏的好友页面,并定位好友搜索框,向搜索框传递要搜索的好友

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LmF5oul5LiN6L-H5oCd5b-1772e,size_20,color_FFFFFF,t_70,g_se,x_16

 :部分iframe没有id或name,用xpath定位

browser.switch_to.default_content()  # 登陆完后回到默认框架
time.sleep(1)
browser.find_element(By.XPATH,'//*[@id="aMyFriends"]').click()
time.sleep(1)
element1 =browser.find_element(By.XPATH,'//[@id="app_container"]/iframe')
browser.switch_to.frame(element1)
ff=browser.find_element(By.XPATH,'//*[@id="qz-search-box-input"]')
ff.send_keys(friend)
time.sleep(1)
browser.switch_to.default_content()
element2=browser.find_element(By.XPATH,'//[@id="app_container"]/iframe')
browser.switch_to.frame(element2)
browser.find_element(By.XPATH,'//*[@id="qz-search-box-result"]/li/div[2]/p').click()
time.sleep(1)
browser.find_element(By.XPATH,'//[@id="mecarewho_list"]/li/div[2]/div[2]/p/a').click()time.sleep(1)
#进入好友的页面

实现效果:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LmF5oul5LiN6L-H5oCd5b-1772e,size_20,color_FFFFFF,t_70,g_se,x_16

 接下来就是进入好友留言板进行留言

注意的是留言框和发表按钮在不同的frame,发表在外面的一层,仔细查看

windows = browser.window_handles
browser.switch_to.window(windows[-1])
time.sleep(1)
browser.find_element(By.XPATH,'//*[@id="friendship_promote_layer"]/table/tbody/tr[1]/td[2]/a').click()
time.sleep(1)
#browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
browser.find_element(By.XPATH,'//*[@id="menuContainer"]/div/ul/li[4]').click()#或者  browser.find_element(By.XPATH,"//div[@id='layBackground']//li[@class = 'menu_item_334']//a[text()='留言板']").click()
time.sleep(3)#进入留言板
browser.switch_to.frame('tgb')
time.sleep(1)
browser.switch_to.frame('veditor1_Iframe')
time.sleep(1)
ff=browser.find_element(By.XPATH,'/html/body')#留言框
ff.send_keys(word)
browser.switch_to.default_content()
browser.switch_to.frame('tgb')
dd=browser.find_element(By.XPATH,'//*[@id="btnPostMsg"]')
dd.click()#确认发表按钮
print("留言成功!!!")
time.sleep(2)
browser.quit()

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LmF5oul5LiN6L-H5oCd5b-1772e,size_18,color_FFFFFF,t_70,g_se,x_16

 python小白,有错误的的地方还请多多指教

完整代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from selenium.webdriver import ActionChains
from selenium.webdriver import ChromeOptions
# 请输入好友和留言内容
qq=input('输入自己的QQ号:')
friend = input('请输入好友:')
word = input('请输入留言内容:')
# 创建一个模拟浏览器对象,然后通过对象去操作浏览器
option=ChromeOptions()
option.add_argument('--headless')
option.add_argument('--disable-gpu')
s=Service("chromedriver.exe")
browser = webdriver.Chrome(service=s,options=option)
browser.get('https://qzone.qq.com/')
browser.maximize_window()
time.sleep(2)
 
browser.switch_to.frame('login_frame')
a_tag = browser.find_element(By.ID,"switcher_plogin")
a_tag.click()
userName_tag = browser.find_element(By.ID,'u')
password_tag =browser.find_element(By.ID,'p')
time.sleep(1)
userName_tag.send_keys(qq)
time.sleep(1)
password_tag.send_keys('此处输入自己的密码')
time.sleep(1)
btn = browser.find_element(By.ID,'login_button')
btn.click()
 
browser.switch_to.default_content()  # 登陆完后回到默认框架
time.sleep(1)
browser.find_element(By.XPATH,'//*[@id="aMyFriends"]').click()
time.sleep(1)
element1 =browser.find_element(By.XPATH,'//*[@id="app_container"]/iframe')
browser.switch_to.frame(element1)
ff=browser.find_element(By.XPATH,'//*[@id="qz-search-box-input"]')
ff.send_keys(friend)
time.sleep(1)
browser.switch_to.default_content()
element2 =browser.find_element(By.XPATH,'//*[@id="app_container"]/iframe')
browser.switch_to.frame(element2)
browser.find_element(By.XPATH,'//*[@id="qz-search-box-result"]/li/div[2]/p').click()#难点
time.sleep(1)#搜索ok
browser.find_element(By.XPATH,'//*[@id="mecarewho_list"]/li/div[2]/div[2]/p/a').click()
time.sleep(1)#进入好友
# 获得打开的第一个窗口句柄
windows = browser.window_handles
browser.switch_to.window(windows[-1])
time.sleep(1)
browser.find_element(By.XPATH,'//*[@id="friendship_promote_layer"]/table/tbody/tr[1]/td[2]/a').click()
time.sleep(1)
#browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
browser.find_element(By.XPATH,'//*[@id="menuContainer"]/div/ul/li[4]').click()#或者  browser.find_element(By.XPATH,"//div[@id='layBackground']//li[@class = 'menu_item_334']//a[text()='留言板']").click()
time.sleep(3)#进入留言板
browser.switch_to.frame('tgb')
time.sleep(1)
browser.switch_to.frame('veditor1_Iframe')
time.sleep(1)
ff=browser.find_element(By.XPATH,'/html/body')#留言框
ff.send_keys(word)
browser.switch_to.default_content()
browser.switch_to.frame('tgb')
dd=browser.find_element(By.XPATH,'//*[@id="btnPostMsg"]')
dd.click()#确认发表按钮
print("留言成功!!!")
time.sleep(2)
browser.quit()

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们