先下载chrome或者firefox驱动
以下地址下载地址是Chrome驱动的地址
Chrome历史版本下载:https://www.chromedownloads.net/chrome64linux-stable/
http://chromedriver.storage.googleapis.com/index.html
http://chromedriver.chromium.org/(这个地址需要国际网络环境)
firefox的驱动下载地址,自己找对应的版本
firefox历史版本下载地址:http://ftp.mozilla.org/pub/firefox/releases/
https://github.com/mozilla/geckodriver/releases
https://selenium-python.readthedocs.io/installation.html#drivers
1.下载自己浏览器对应 版本。
安装如果你的环境是conda那就将下载的驱动放到 conda安装目录下的Script目录中就行,放其他目录也可以,但是要将驱动目录加入环境变量。
2.另一种安装方法,可以不用将驱动地址放入环境变量,指定驱动路径。
driver = webdriver.Firefox(executable_path=firefox_driver_path)
driver = webdriver.Chrome(executable_path=chrome_driver_path)
一般下载驱动的时候会有提示,驱动的版本支持的浏览器版本,一定要下载对应的驱动。
然后就可以用了#这是一个最简单的示例,这个示例是默认将驱动加入了环境变量的路径了,所以不用指定驱动路径。
from selenium import webdriver
options = Options()
profile = FirefoxProfile()
profile.set_preference('general.useragent.override', "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36")
driver = webdriver.Firefox(options=options, firefox_profile=profile)
driver.get("https://www.xcwmoon.com")
#其他高级用法可以看它的文档。只是一个示例,需要的包要单独导入
Chrome示例:options = webdriver.ChromeOptions()
options.add_argument('User-Agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"')
browser = webdriver.Chrome(options=options)