使用webdriver驱动浏览器时,不能直接写参数

在使用selenium的webdriver调用浏览器时,最好不要直接写变量参数的名称,前边加上   executable_path=   表示指定了驱动器的路径。

from selenium import webdriver

url = 'https://www.baidu.com/'
chrome = r'/Users/chensihan/Downloads/chromedriver'

# executable_path指定了chromedriver所在的路径,程序运行时就会到该路径下启动chrome浏览器
# 最好在参数chrome前加上executable_path=,否则有可能报错
driver = webdriver.Chrome(executable_path=chrome)

driver.get(url=url)
driver.find_element_by_id('kw').send_keys('大锤')
driver.find_element_by_id('su').click()

driver.close()

结束

原文地址:https://www.cnblogs.com/caoyinshan/p/12120919.html