(笔记) selenium 在win10下安装PhantomJS 和 webdriver

1,查看google浏览器的版本,在设置下面有个帮助里免邮个关于chrome,可以看到版本.
2,去 http://chromedriver.storage.googleapis.com/index.html 下载对应chorme的webdriver,
3,解压后放到python安装目录下:查看路径办法,按win键输入python,查打开文件位置,这是个快捷方式,右键查看属性,然后在属性里找到起始位置,(我的 C:\Users~mumu~ \AppData\Local\Programs\Python\Python37,不想找可以试试是不是这里),复制打开位置,copy来webdriver,
4,同理下载(https://phantomjs.org/download.html)phantomJS,在bin文件夹里有个.exe文件,copy到python安装目录下

这样就可以去用他们调用chorme了,以下是示范代码:


from selenium import webdriver

# driver =webdriver.Chrome()
# driver =webdriver.PhantomJS(executable_path=r"C:\Users\wangpengyi_wb\Downloads\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin\phantomjs.exe") #这个意思是 不用复制到python安装目录的话就要这样写个路径,chrome的driver也能这样写

driver =webdriver.PhantomJS()

driver.get("https://www.douban.com/")

ret4 = driver.find_elements_by_tag_name("h1")
print(ret4[0].text)
#输出:豆瓣

ret5 = driver.find_elements_by_link_text("下载豆瓣 App")
print(ret5[0].get_attribute("href"))
#输出:https://www.douban.com/doubanapp/app?channel=nimingye

driver.close()  #如果用chormedriver的话,想看完整浏览器打开过程,这里需要专门关闭,无头浏览器则不用,chorme也可以设置成不打开浏览器.

phantomjs据说已经不维护了,要用chromedriver的headless功能要这样写:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options



# 打开浏览器
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

# 创建浏览器对象
driver = webdriver.Chrome( chrome_options=chrome_options)

driver.get("https://www.douban.com/")

ret4 = driver.find_elements_by_tag_name("h1")
print(ret4[0].text)
#输出:豆瓣

ret5 = driver.find_elements_by_link_text("下载豆瓣 App")
print(ret5[0].get_attribute("href"))
#输出:https://www.douban.com/doubanapp/app?channel=nimingye
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值