python3+selenium启动火狐浏览器和chrome----2019年最新

版权声明:转载请注明出处 https://blog.csdn.net/qq_33289175/article/details/88080509

1、使用pip安装 selenium

pip install selenium

2、下载geckodriver

下载地址        https://u19511066.ctfile.com/fs/19511066-344520795(点击普通下载就行)

3、将里面的geckodriver文件放在放置在firefox安装路径里(和Firefox.exe在一起),并将firefox安装路径添加到环境变量

4、写代码

#coding:utf-8
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.2345.com/?k32421596")

#在百度搜索框中输入关键字"python"

driver.find_element_by_xpath("//div[@id='J_schInbox']/input").send_keys("python")
#单击搜索按钮
driver.find_element_by_id("j_search_sbm").click()

到这就可以打开火狐浏览器了,selenium元素定位的一些方法需要自己多学习,多用自然就会了

如果遇到报错,看这里

https://blog.csdn.net/qq_33289175/article/details/88080794

启动chrome就简单了,不需要配置环境变量:

下载chromedriver

https://u19511066.ctfile.com/fs/19511066-344520051(点击普通下载就行)

1、将chromedriver放到chrome安装路径中,记住存放路径

扫描二维码关注公众号,回复: 5405648 查看本文章

2、写代码

#coding:utf-8
from selenium import webdriver

chromedriver="G:/Users/loong/AppData/Local/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(chromedriver)

有什么不懂的问题可以留言,有时间我可以解答一下,至少可以让后来的人看到

猜你喜欢

转载自blog.csdn.net/qq_33289175/article/details/88080509