selenium 相关操作

selenium基础用法.py

from selenium import webdriver
from lxml import etree
from time import sleep

实例化浏览器对象

bro = webdriver.Chrome(executable_path='./chromedriver')

让浏览器向指定的url 发起请求

bro.get('http://scxk.nmpa.gov.cn:81/xk/')

获取当前浏览器页面源码数据

page_text = bro.page_source

tree = etree.HTML(page_text)
li_list = tree.xpath('//ul[@id="gzlist"]/li')
for li in li_list:
title = li.xpath('./dl/@title')[0]
print(title)
sleep(5)
bro.quit()

selenium其他自动化操作.py

from selenium import webdriver
from time import sleep
from lxml import etree
bro = webdriver.Chrome(executable_path='./chromedriver.exe')

bro.get('https://www.taobao.com/')

标签定位

search_input = bro.find_element_by_id('q')

标签交互

search_input.send_keys('Iphone')

执行一组js 代码

bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(2)
btn = bro.find_element_by_css_selector('.btn-search')

点击搜索按钮

btn.click()
bro.get('https://wwww.baidu.com')
sleep(2)

回退

bro.back()
sleep(2)

前进

bro.forward()

sleep(5)
bro.quit()

posted @ 2021-02-25 23:51  未来全栈攻城狮  阅读(69)  评论(0编辑  收藏  举报