H5W3
当前位置:H5W3 > 收藏夹 > 正文

提示框、滚动条处理与JS的应用

# 提示框的处理
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
driver.maximize_window()
driver.implicitly_wait(10)
# 实战1 了解什么是JS脚本
# driver.execute_script('alert("爱生活爱绅士的hero~");')
# time.sleep(2)
# driver.switch_to.alert.accept() # 确认提示内容

# w1 = driver.find_element_by_name('wd')
# driver.execute_script("arguments[0].style.border='5px solid red'",w1) # 加边框

# 实战2 :通过js脚本获取元素属性
# id = driver.execute_script("return document.getElementById('kw').getAttribute('id')")
# name = driver.execute_script("return document.getElementById('kw').getAttribute('name')")
# print(id,name)

# 实战3 滚动条处理
# driver.find_element_by_name('wd').send_keys('python滚动条')
# driver.find_element_by_id('su').click()
# time.sleep(2)
# 3.1相对滚
# js = "window.scrollBy(0,300)" # 向下滚
# driver.execute_script(js)
# time.sleep(3)
# js = "window.scrollBy(0,1000)"
# driver.execute_script(js)
# time.sleep(2)
# js = "window.scrollBy(0,-500)" # 向上滚
# driver.execute_script(js)
# 封装滚动条
# def scoll(driver,heigh):
# js = "window.scrollBy(0,"+str(heigh)+")"
# driver.execute_script(js)
# time.sleep(2)
# scoll(driver,500)
# scoll(driver,800)
# scoll(driver,-1000)

# 实战4 通过js脚本改变元素属性
# js = 'arguments[0].removeAttribute("value");'
# js = 'arguments[0].setAttribute("value","jasonchen");'
w1 = driver.find_element_by_name('wd')
# js = 'arguments[0].setAttribute("value","jasonchen");' # arguments:参数列表
# driver.execute_script(js,w1)
js = 'arguments[0].setAttribute("jasonchen","qiumei");'
driver.execute_script(js,w1)

# time.sleep(5)
# driver.close()

本文地址:H5W3 » 提示框、滚动条处理与JS的应用

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址