selenium模块的而简单使用

一.seleniu的简单使用

'''
selenium:基于浏览器的自动化操作模块
    通过代码定制一些浏览器自动化操作,然后把该操作作用到浏览器
    1.pip install selenium
    2.导包:from selenium import webdriver
    3.下载浏览器的驱动程序
        下载地址:http://chromedriver.storage.googleapis.com/index.html
        版本的映射关系表:https://blog.csdn.net/huilan_same/article/details/51896672
    4.实例化一个浏览器对象(驱动程序)
    5.通过代码指定行为动作
'''
import time
from selenium import webdriver
#实例化一个浏览器对象
bro=webdriver.Chrome(executable_path='./chromedriver.exe')
time.sleep(2)
#浏览器获取页面
bro.get('http://baidu.com/')
time.sleep(2)
# 浏览器输入找到输入框,并输入数据
text_input=bro.find_element_by_id('kw')
text_input.send_keys('我草')
time.sleep(2)
#浏览器找到点击标签,并点击
btn=bro.find_element_by_id('su')
btn.click()

time.sleep(2)
#浏览器向下拖动滚动条
js='window.scrollTo(0,document.body.scrollHeight)'
bro.execute_script(js)
#获取当前浏览器显示的页面数据
page_text=bro.page_source#页面数据也包含动态加载出来的数据
print(page_text)

time.sleep(5)
bro.quit()

猜你喜欢

转载自www.cnblogs.com/tjp40922/p/10446752.html