python使用selenium

78 阅读1分钟

安装

pip install selenium

问题

安装谁都会,但是可能运行的时候会报一个错

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 89

Current browser version is 91.0.4472.77 with binary path

这个错是因为驱动版本不一致的问题,下载对应的的Chrome Driver版本就可以了。

我使用的是mac

下载好以后我放到/usr/local/bin

然后还执行了

xattr -d com.apple.quarantine chromedriver   // 权限问题所以执行了这行命令

image.png

然后就可以正常用了

参考文档:blog.csdn.net/adorable_/a…

chrome driver:googlechromelabs.github.io/chrome-for-…

from selenium import webdriver  
# 启动浏览器  
# 中文文档 https://python-selenium-zh.readthedocs.io/zh-cn/latest/1.%E5%AE%89%E8%A3%85/  
# 官网 https://www.selenium.dev/zh-cn/documentation/webdriver/  
# /Applications/wpsoffice.app/Contents/MacOS/wpsoffice  
# /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome  
# https://googlechromelabs.github.io/chrome-for-testing/ chrome-driver下载地址  
driver = webdriver.Chrome() # 这里使用Chrome浏览器,可以根据需要选择其他浏览器  
  
  
# 打开网站  
driver.get("https://www.baidu.com") # 替换成你要访问的网站  
# driver.implicitly_wait(0.5)  
# text_box = driver.find_element(by=By.NAME, value="my-text")  
# submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")  
# text_box.send_keys("Selenium")  
# submit_button.click()