#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)
'''
from selenium import webdriver

import time,os

# about:addons 火狐浏览器安装组件,访问的地址

# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
#id
keys="selenium自动化"
delay=3
# url="https://www.cnblogs.com/nicetime"
# url="https://www.12306.cn/index/"
url="file:///D:/ideaSpace/autoProject/python_autotest/nicetime/webdriver/js12.html"
driver=webdriver.Firefox()

driver.get(url)

#回到底部
js2="var q=document.documentElement.scrollTop=10000"
driver.execute_script(js2)

js22="window.scrollTo(0,document.body.scrollHeight)"
driver.execute_script(js22)

time.sleep(delay)

#回到顶部
js1="var q=document.documentElement.scrollTop=0"

# chrome浏览器
# js11="var q=document.body.scrollTop=0"
driver.execute_script(js1)

js12="window.scrollTo(0,0)"
driver.execute_script(js12)

# #左右滚动
js3="window.scrollTo(100,400)"
driver.execute_script(js3)

# #聚焦元素 find_element_by系列
target=driver.find_element_by_id("blog_nav_sitehome")
driver.execute_script("arguments[0].scrollIntoView();",target)

# 兼容浏览器
def scroll_Top():
if driver.name=="chrome":
js="var q=document.body.scrollTop=0"
else:
js="var q=document.documentElement.scrollTop=0"
return driver.execute_script(js)

def scroll_down():
if driver.name=="chrome":
js="var q=document.body.scrollTop=10000"
else:
js="var q=document.documentElement.scrollTop=10000"
return driver.execute_script(js)

#滚动高度 滚动宽度
# document.body.scrollHeight
# document.body.scrollWidth

#日历处理-输入指定的日期
# <input type="text" class="input" value="2018-07-21" id="train_date" readonly="">
#去掉只读属性
js1='document.getElementById("train_date").removeAttribute("readonly");'
driver.execute_script(js1)

time.sleep(delay)

#输入日期
date="2018-12-04"
js2='document.getElementById("train_date").value="%s"'%date
driver.execute_script(js2)

time.sleep(delay)

#清空文本框后输入值
driver.find_element_by_id("train_date").clear()
driver.find_element_by_id("train_date").send_keys(date)

time.sleep(delay)


#内嵌div
# 纵向底部 顶部
js3='document.getElementById("ideaic").scrollTop=10000'
driver.execute_script(js3)
time.sleep(delay)

js4='document.getElementById("ideaic").scrollTop=0'
driver.execute_script(js4)
time.sleep(delay)


# 横向右侧 左侧
js5='document.getElementById("ideaic").scrollLeft=10000'
driver.execute_script(js5)
time.sleep(delay)

js6='document.getElementById("ideaic").scrollLeft=0'
driver.execute_script(js6)
time.sleep(delay)


#用class定位
js7='document.getElementsByClassName("scroll")[0].scrollTop=10000'
driver.execute_script(js7)
time.sleep(delay)

js8='document.getElementsByClassName("scroll")[0].scrollLeft=10000'
driver.execute_script(js8)
time.sleep(delay)


driver.quit()
posted on 2018-12-04 22:08  新美好时代  阅读(225)  评论(0编辑  收藏  举报