mac selenium报错selenium.common.exceptions.WebDriverException: Message: 'Unexpected error launching In

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
browser.find_element_by_id("kw").send_keys("selenium")
browser.find_element_by_id("su").click()
browser.quit()

报错信息:

selenium.common.exceptions.WebDriverException: Message: 'Unexpected error launching Internet Explorer.

或者

FileNotFoundError: [Errno 2] No such file or directory

 

解决方法

安装浏览器驱动:https://docs.seleniumhq.org/download/
谷歌或者火狐,按需下载

mac selenium报错selenium.common.exceptions.WebDriverException: Message: 'Unexpected error launching In_第1张图片

1.通过指定驱动位置打开浏览器

将解压出来的驱动文件放任意位置,传一个executable_path,将驱动的绝对路径赋值给 executable_path

代码如下

from selenium import webdriver

browser = webdriver.Chrome(executable_path = '/usr/local/bin/chromedriver')
browser.get('http://www.baidu.com/')
browser.find_element_by_id("kw").send_keys("selenium")
browser.find_element_by_id("su").click()
browser.quit()

 

2.将驱动文件放在python的bin目录下

按住cmmand(windows系统是按住ctrl键)点击Chrome,发现executable_path传了一个默认值。

mac selenium报错selenium.common.exceptions.WebDriverException: Message: 'Unexpected error launching In_第2张图片

 

mac selenium报错selenium.common.exceptions.WebDriverException: Message: 'Unexpected error launching In_第3张图片

因此,将驱动文件放在python的bin目录下,不需要传executable_path,也能打开浏览器。

查看python安装路径,如下

import sys

print(sys.path)

mac selenium报错selenium.common.exceptions.WebDriverException: Message: 'Unexpected error launching In_第4张图片

/Library/Frameworks/Python.framework/Versions/3.6  驱动文件放在该路径下的bin文件夹下面就欧克啦~

 

 

 

你可能感兴趣的:(selenium,问题记录)