1. 程式人生 > >Selenium3+webdriver學習筆記12(js操作應用:滾動條 日曆 內嵌div)

Selenium3+webdriver學習筆記12(js操作應用:滾動條 日曆 內嵌div)

#!/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()