安装selenium包,安装Chrome浏览器,安装chromedriver
Mac下相关安装:
pip install selenium
使用selenium打开一个空的浏览器窗口:
from selenium import webdriver
driver = webdriver.Chrome()
报错’chromedriver’ executable needs to be in PATH
解决方法一:直接下载chromedriver并添加路径
1.下载chromedriver,查看chrome浏览器版本为62
下载地址:http://npm.taobao.org/mirrors/chromedriver/2.33/ 版本要对应。
下载成功后,无需安装
2.添加chromedriver路径,再次运行:
from selenium import webdriver
driver =webdriver.Chrome(’/Users/username/Downloads/chromedriver’)
打开网页成功,问题解决!
完整示例源码:百度自动搜索
import time
# from selenium.webdriver.common.keys import Keys
from selenium import webdriver
driver = webdriver.Chrome("/Users/xiaohuangrenruanjian/Downloads/chromedriver")
driver.get("http://baidu.com/")
time.sleep(3)
input_element = driver.find_element_by_id("kw")
input_element.send_keys("wjsou.com")
# input_element.send_keys(Keys.ENTER)
click = driver.find_element_by_id("su")
click.click()
time.sleep(3)
# if driver.page_source.find("密码错误") == -1:
# print("登陆成功")
# else:
# print("登陆失败")
print(driver.page_source)
driver.close()
作者:weixin_39929640