Selenium自动化实战学习第二天
2018-09-03 本文已影响11人
胆小的米老鼠
操作cookies
#添加cookies
import time
from selenium import webdriver
driver = webdriver.Firefox()
url= 'http://www.baidu.com'
driver.get(url)
driver.add_cookie({'name':'123','value':'333'})
cookies = driver.get_cookies()
for cookie in cookies:
print('%s ->%s' % (cookie['name'],cookie['value']))
print('cookies 操作完成')
time.sleep(2)
driver.quit()
操作滚动条
#调用javascript拖动滚动条
from selenium import webdriver
import time
driver = webdriver.Firefox()
url= 'http://www.baidu.com'
driver.get(url)
#搜索
time.sleep(2)
driver.find_element_by_id('kw').send_keys('senlenium')
time.sleep(3)
driver.find_element_by_id('su').click()
#滚动条拖到底部
js ="document.documentElement.scrollTop=10000"
time.sleep(3)
driver.execute_script(js)
time.sleep(3)
#滚动条拖动到顶部
js= "document.documentElement.scrollTop=0"
driver.execute_script(js)
#左右拖动
#window_scro||to(左边距,上边距)
# js = "window_scro||to(200,1000)"
# driver.execute_script(js)
time.sleep(3)
driver.quit()
获取服务端与客户端的交互信息
from selenium import webdriver
import time
import logging
#开启debug获取服务端与客户端的交互信息
logging.basicConfig(level=logging.DEBUG)
driver = webdriver.Firefox()
url= 'http://zhtb.hcstec.com/login/login.html'
driver.get(url)
driver.find_element_by_xpath('/html/body/div/div/form/div[1]/select').click()
time.sleep(2)
driver.find_element_by_xpath('/html/body/div/div/form/div[1]/select/option[2]').click()
time.sleep(1)
driver.find_element_by_xpath('/html/body/div/div/form/div[2]/input').send_keys('142630170611011230')
time.sleep(1)
driver.find_element_by_xpath('/html/body/div/div/form/div[3]/input').send_keys('123456')
time.sleep(2)
driver.find_element_by_xpath('/html/body/div/div/form/button').click()