python + selenium 驱动PhantomJS,Firefox,Chrome

PhantomJS是一个无界面浏览器,可以通过js代码执行请求逻辑。但是不支持鼠标悬停事件,不支持flash。

PhantomJS下载地址:https://npm.taobao.org/dist/phantomjs/

1.如果用python+selenium 驱动PhantomJS 那么selenium 版本必须是2.x

打开一个朋友圈文章url

from selenium import webdriver
url="http://mp.weixin.qq.com/s?__biz=MzAwMTc4OTU4Nw==&mid=2651582976&idx=2&sn=ba9f54af181837b34ce6985aa143c526&chksm=812b09fcb65c80eaba69bff9caf673339ad724de000c9202531bf381602b0b037ea0ededa5c6&mpshare=1&scene=23&srcid=0218CtB06UANo9gFuW5YIIV7#rd"
browser = webdriver.PhantomJS(executable_path=r"D:\Python\phantomjs-2.1.1-windows\bin\phantomjs.exe");
browser.get(url)
element0 =browser.find_element_by_tag_name("iframe")
iurl =element0.get_attribute('src')
browser.get(iurl)
element = browser.find_elements_by_class_name("txp_btn_play")
print(browser.page_source)

打印html中显示不支持flash。

2.python+selenium 驱动Firefox,需要一个火狐浏览器驱动。并且火狐的版本必须是27的,可以把现在火狐卸载,在安装这个,把驱动放在浏览器同目录。

驱动地址:https://github.com/mozilla/geckodriver/releases

火狐下载地址:http://ftp.mozilla.org/pub/firefox/releases/27.0b9/win32/zh-CN/Firefox%20Setup%2027.0b9.exe

再次打开这个地址

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
url="http://mp.weixin.qq.com/s?__biz=MzAwMTc4OTU4Nw==&mid=2651582976&idx=2&sn=ba9f54af181837b34ce6985aa143c526&chksm=812b09fcb65c80eaba69bff9caf673339ad724de000c9202531bf381602b0b037ea0ededa5c6&mpshare=1&scene=23&srcid=0218CtB06UANo9gFuW5YIIV7#rd"
binary = FirefoxBinary(r"E:\Mozilla Firefox\firefox.exe")
browser = webdriver.Firefox(executable_path=r"E:\Mozilla Firefox\geckodriver.exe",firefox_binary=binary);
browser.get(url)
element0 =browser.find_element_by_tag_name("iframe")
iurl =element0.get_attribute('src')
browser.get(iurl)

这里我们打开的视频播放页面,但是播放按钮是flash代码,在火狐中没有明确显示播放按钮的标签,不能通过找到html元素在执行点击事件这种方法。

但是手动点击播放按钮是可以播放的。通过移动鼠标到播放按钮上点击,需要知道播放按钮的位置,不能确定,所以ActionChains(browser).move_by_offset(18,562).click() 是不可以了。

3.python+selenium 驱动Chrome,需要一个谷歌浏览器驱动。我用的谷歌版本66,python是3.6,前面为了调用PhantomJS 把selenium版本换到了2.x,现在却不可以调用谷歌,换到3.x,就可以了,谷歌驱动与谷歌的版本也有关系,我用的驱动版本是2.37

驱动地址:https://npm.taobao.org/mirrors/chromedriver

打开同一个页面

from selenium import webdriver  
from selenium.webdriver.common.keys import Keys  

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time

#加载谷歌驱动
browser = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
#微信上的一个网页,有一个视频
url="http://mp.weixin.qq.com/s?__biz=MzAwMTc4OTU4Nw==&mid=2651582976&idx=2&sn=ba9f54af181837b34ce6985aa143c526&chksm=812b09fcb65c80eaba69bff9caf673339ad724de000c9202531bf381602b0b037ea0ededa5c6&mpshare=1&scene=23&srcid=0218CtB06UANo9gFuW5YIIV7#rd"
#打开url
browser.get(url)
#睡眠,等待页面加载完成
time.sleep(4)
#获取iframe标签,视频窗口
element0 =browser.find_element_by_tag_name("iframe")
#获得标签属性
iurl =element0.get_attribute('src')
#加载iframe中的url
browser.get(iurl) 
#找到播放按钮
element = browser.find_elements_by_class_name("txp_btn_play")#txp_btn
#点击播放按钮 
element[0].click()

使用谷歌浏览器,可以得到flash生成的播放按钮的html标签,而火狐就不行,点击这个元素,让视频自动播放。

打印log

for entry in browser.get_log('performance'):  
    print (dir(entry)) 

ok,现在已经完成了驱动浏览器播放flash视频。


猜你喜欢

转载自blog.csdn.net/qq_31683775/article/details/80051040