from selenium import webdriver
cap = webdriver.DesiredCapabilities.PHANTOMJS.copy()
# 修改driver配置文件,可选,关闭图片,打开磁盘缓存,设置User-Agent
cap["phantomjs.page.settings.loadImages"] = False
cap["phantomjs.page.settings.disk-cache"] = True
cap['phantomjs.page.customHeaders.User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36'
web = webdriver.PhantomJS(desired_capabilities=cap)
# 下列命令接口的hack参考:https://stackoverflow.com/a/28269099
# 添加Phantomjs的命令接口
web.command_executor._commands['phantomjs_script'] = ('POST', '/session/$sessionId/phantom/execute')
# 添加访问命令接口的方法
def execute_phantomjs_script(script, *args):
return web.execute('phantomjs_script', {'script': script, 'args': args})
web.execute_phantomjs_script = execute_phantomjs_script
# 调用phantomjs的API接口
# 具体API参考:http://phantomjs.org/api/webpage/
# `this`对象等价于JS代码`var this = require('webpage').create();`
# 查看现有配置
print web.execute_phantomjs_script('return this')
selenium添加phantomjs的js API接口
最新推荐文章于 2023-09-23 12:59:41 发布