怎么用python启动谷歌浏览器_Python3.X使用Selenium驱动ChromeDriver自动化操作浏览器...

一、安装seleniumpip install selenium

二、下载谷歌和webdriver浏览器驱动文件

下载安装原生谷歌浏览器chrome.exe并且安装

下载webdriver驱动文件

https://sites.google.com/a/chromium.org/chromedriver/downloads

default

点开后选择

default

下载完成解压里面的webdrvier.exe到python的安装目录里,python的安装目录要加入系统的环境变量里

default

三、启动浏览器并打开指定网址from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

class AutoSearch():

"""docstring for AutoSearch"""

def __init__(self):

self.options = webdriver.ChromeOptions();

#chrome 启动初始位置

self.options.add_argument('--window-position=0,0');

#chrome 启动初始大小

self.options.add_argument('--window-size=1080,800');

self.browser=webdriver.Chrome(chrome_options=self.options)

self.browser.get("https://www.baidu.com")

obj=AutoSearch()

执行上面代码后就会打开百度首页面

default

四、使用selenium-webdriver提供的功能操作网页

一个完整的打开百度搜索python的功能from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

import time

class AutoSearch():

"""docstring for AutoSearch"""

def __init__(self):

self.options = webdriver.ChromeOptions();

#chrome 启动初始位置

self.options.add_argument('--window-position=0,0');

#chrome 启动初始大小

self.options.add_argument('--window-size=1080,800');

self.browser=webdriver.Chrome(chrome_options=self.options)

self.browser.get("http://www.sf-express.com/cn/sc/dynamic_function/waybill/#search/bill-number/964604199776")

#通过id查找输入框输入文字

def input_by_id(self, text=u"", element_id=""):

#通过 id 查找网页元素

input_el = self.browser.find_element_by_id(element_id)

input_el.clear()

#输入字符串

input_el.send_keys(text)

time.sleep(0.5)

#通过id查找元素并单击

def click_by_id(self, element_id=""):

search_el = self.browser.find_element_by_id(element_id)

#鼠标左键单击

search_el.click()

time.sleep(0.5)

#通过类名查找元素并单击

def click_by_class(self, element_class=""):

#通过 class 查找网页元素

search_el = self.browser.find_element_by_class_name(element_class)

#鼠标左键单击

search_el.click()

time.sleep(0.5)

obj=AutoSearch()

obj.input_by_id('python','kw')

obj.click_by_id('su')

用法中文文档可以参考 http://selenium-python-docs-zh.readthedocs.io/zh_CN/latest/

英文文档 api http://www.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值