selenium+python模拟浏览器进入好友QQ空间留言

我们要模拟浏览器登陆qq空间并进入好友空间留言,首先要安装自动化测试工具selenium,它支持多种浏览器,我这里使用的是谷歌浏览器。使用谷歌浏览器需要下载chromedriver.exe,驱动版本要和浏览器版本对应,网上搜索即可。最好将该驱动和chrome.exe放在一个文件夹下,并在电脑环境path中将该路径添加进去,不然程序可能报错:'chromedriver' executable needs to be in PATH。
首先介绍安装和导入selenium模块,安装selenium直接用pip安装即可:

pip install selenium

导入:

from selenium import webdriver

现在步入正题,首先需要创建一个浏览器对象:

# 创建一个模拟浏览器对象,然后通过对象去操作浏览器
path = r'C:\Users\NiHao\AppData\Local\Google\Chrome\Application\chromedriver.exe'
url = 'https://i.qq.com'

browser = webdriver.Chrome(executable_path=path)
browser.get(url=url)
browser.maximize_window()
time.sleep(2)

path为chromedriver.exe的路径,注意大小写。

之后来到qq登陆的界面,注意我们的登陆框是在一个frame框架里面的,首先要选择该框架才能定位到框架里面的内容。selenium+python模拟浏览器进入好友QQ空间留言_第1张图片

这里选择账号密码登陆,定位到输入位置,并输入内容提交即可。

browser.switch_to.frame('login_frame')  # 选择框架

browser.find_element_by_id('switcher_plogin').click()
time.sleep(2)

account = browser.find_element_by_id('u')
account.send_keys('账号')
password = browser.find_element_by_id('p')
password.send_keys('密码')
time.sleep(1)
denglu = browser.find_element_by_id('login_button').click()

browser.switch_to.default_content()  # 登陆完后回到默认框架
time.sleep(5)

 

之后我们便来到了自己的空间,现在我们的目标是通过,点击上方的好友显示出下方的搜索框,在搜索框中填入要进入空间好友的名字,再点击下方的结果就可以进入好友的空间了。这里要注意延时,等待网页加载出信息。

selenium+python模拟浏览器进入好友QQ空间留言_第2张图片

# 搜索好友
browser.find_element_by_id('aMyFriends').click()
time.sleep(1)
browser.find_element_by_id('friend_search_input').send_keys(friend)
time.sleep(2)
browser.find_element_by_xpath('//*[@id="search_friend_result"]/li/a/img').click()
time.sleep(5)

 

进入好友空间后,你会发现现在除了你自己的空间窗口,现在又多出了一个窗口。再这之后的操作都是在第二个窗口中操作的,因此必须先切换到第二个窗口。我在问题困了半天,如果不切换窗口是定位不到好友空间的元素的。

# 切换到好友空间的窗口
windows = browser.window_handles
browser.switch_to.window(windows[-1])

有时你甚至会发现还会有提示你查看好友亲密度的坑爹操作,这时就需要先,“点击”  “我知道了” 才能操作后面的内容。这里可以用try--except进行判断,有就执行“点击”,没有的话会升起一个异常,直接pass就行。

# 判断是否有查看亲密度的坑爹操作,有就点,没有就pass
try:
    browser.find_element_by_xpath('//*[@id="friendship_promote_layer"]/table/tbody/tr[1]/td[2]/a').click()
except common.exceptions.NoSuchElementException:
    pass

这里的NoSuchElementException需要先导入common模块

from selenium import common

 

好了,现在我们终于来到好友的空间准备留言了,定位到留言板,click,问题不大,JavaScript直接加载到留言窗口,之后只用再定位到留言窗口,写入留言就行了。等等,留言窗口的xpath怎么是 :/html/body? 仔细看看可以看到这个窗口是嵌入在两层frame里的,依次选定后才能定位到窗口,写入留言内容。

browser.switch_to.frame('tgb')
time.sleep(1)
browser.switch_to.frame('veditor1_Iframe')
time.sleep(1)
browser.find_element_by_xpath('/html/body').send_keys(content)
time.sleep(1)

最后一步点击发表,需要注意的是,发表按钮又是在窗口的外面一层frame,因此需要先退出来,再进入该frame,最后点击发表留言。

# 回到上一层框架再点提交留言按钮
browser.switch_to.default_content()
browser.switch_to.frame('tgb')
browser.find_element_by_xpath('//*[@id="btnPostMsg"]').click()

至此,终于大功告成了!留言成功!扩展一下每天定时给女朋友留言也是可以的,完整代码如下:

from selenium import webdriver
from selenium import common
import time

# 请输入好友和留言内容
friend = input('请输入好友:')
content = input('请输入留言内容:')

# 创建一个模拟浏览器对象,然后通过对象去操作浏览器
path = r'C:\Users\NiHao\AppData\Local\Google\Chrome\Application\chromedriver.exe'
url = 'https://i.qq.com'

browser = webdriver.Chrome(executable_path=path)
browser.get(url=url)
browser.maximize_window()
time.sleep(2)

# 输入账号密码,并登陆
browser.switch_to.frame('login_frame')  # 选择框架

browser.find_element_by_id('switcher_plogin').click()
time.sleep(2)

account = browser.find_element_by_id('u')
account.send_keys('账号')
password = browser.find_element_by_id('p')
password.send_keys('密码')
time.sleep(1)
denglu = browser.find_element_by_id('login_button').click()

browser.switch_to.default_content()  # 登陆完后回到默认框架
time.sleep(5)

# 搜索好友
browser.find_element_by_id('aMyFriends').click()
time.sleep(1)
browser.find_element_by_id('friend_search_input').send_keys(friend)
time.sleep(2)
browser.find_element_by_xpath('//*[@id="search_friend_result"]/li/a/img').click()
time.sleep(5)

# 进入好友空间留言
'''# 悬停在 ‘他的主页’
move_xpath = '//*[@id="tb_index_li"]/div[1]/a/span'
more_mue = WebDriverWait(driver=browser, timeout=15).until(EC.visibility_of_element_located((By.XPATH, move_xpath)))
ActionChains(browser).move_to_element(more_mue).perform()
time.sleep(2)'''

# 切换到好友空间的窗口
windows = browser.window_handles
browser.switch_to.window(windows[-1])

# 判断是否有查看亲密度的坑爹操作,有就点,没有就pass
try:
    browser.find_element_by_xpath('//*[@id="friendship_promote_layer"]/table/tbody/tr[1]/td[2]/a').click()
except common.exceptions.NoSuchElementException:
    pass

# 进入好友留言板
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)
browser.find_element_by_xpath('/html/body').send_keys(content)
time.sleep(1)

# 回到上一层框架再点提交留言按钮
browser.switch_to.default_content()
browser.switch_to.frame('tgb')
browser.find_element_by_xpath('//*[@id="btnPostMsg"]').click()

# 关闭浏览器
time.sleep(5)
browser.quit()

 

你可能感兴趣的:(selenium,python)