selenium操作firefox

下载浏览器

下载链接:https://ftp.mozilla.org/pub/firefox/releases/

79.0为版本号,可以替换
https://download-installer.cdn.mozilla.net/pub/firefox/releases/79.0/win64/zh-CN/Firefox%20Setup%2079.0.exe

设置不更新:https://jingyan.baidu.com/article/da1091fb712f5f027949d671.html

下载驱动

https://liushilive.github.io/github_selenium_drivers/md/Firefox.html

隐藏window.navigator.webdriver属性

参考:
https://blog.csdn.net/mighty13/article/details/113575905

https://blog.csdn.net/weixin_43268350/article/details/111030616?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3.pc_relevant_paycolumn_v3&utm_relevant_index=6

profile = webdriver.FirefoxProfile()
profile.set_preference("dom.webdriver.enabled", False)
driver = webdriver.Firefox(options=options,firefox_profile=profile)

检查

js="return window.navigator.webdriver"
result=driver.execute_script(js)

linux环境部署

geckodriver下载地址: https://github.com/mozilla/geckodriver/releases/tag/v0.30.0

chmod +x geckodriver
tar -xvzf geckodriver-v0.30.0-linux64.tar.gz

狐火浏览器下载地址:
https://download-installer.cdn.mozilla.net/pub/firefox/releases/79.0/linux-x86_64/zh-CN/firefox-79.0.tar.bz2

tar   -jxvf    xx.tar.bz2

建立软连接
ln -s /data/spyder_test/firefox/firefox /usr/bin/firefox
rm -rf /usr/bin/firefox

测试


from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time
options = Options()
options.add_argument('-headless') # 无头参数

 
brower = webdriver.Firefox(firefox_options=options,executable_path='./geckodriver')
brower.get("http://www.baidu.com")

brower.find_element_by_id('kw').send_keys('selenium')
brower.find_element_by_id('su').click()

time.sleep(3)
print(brower.current_url)
brower.quit()
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值