WebDriver提供了execute_script()方法来执行JavaScript代码

具体DOM操作使用方法见之前写的一篇博文:javascript:void(0)

关于Webdriver自动化测试之执行JavaScript脚本可直接参考下面代码

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:base.py
@time:2020/10/17
"""
import os

from selenium import webdriver

class Base:
def setup_class(self):
# option = webdriver.ChromeOptions()
# option.add_experimental_option('w3c', False)
# self.driver = webdriver.Chrome(options=option)
browser = os.getenv('browser')
if browser == 'firefox':
self.driver = webdriver.Firefox()
elif browser == 'headless':
self.driver = webdriver.phantomjs()
else:
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(5)

<span ><span >def</span> <span >teardown_class</span>(<span >self</span>):</span>
    self.driver.quit()

 

上面代码为演示代码中from test_selenium.base import Base 导入的代码

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_js.py
@time:2020/10/17
"""
from time import sleep

from test_selenium.base import Base

class TestJS(Base):
# 百度搜索演示
def test_js(self):
self.driver.get('https://www.baidu.com')
self.driver.find_element_by_id('kw').send_keys('selenium测试')
element = self.driver.execute_script('return document.getElementById("su")')
element.click() # 点击搜索
sleep(2)
self.driver.execute_script('document.documentElement.scrollTop=10000') # 页面向下滑动10000个像素
sleep(2)
self.driver.find_element_by_xpath('//*[@id="page"]/div/a[10]').click() # 点击下一页
sleep(2)
self.driver.execute_script('document.documentElement.scrollTop=10000') # 页面向下滑动10000个像素
sleep(2)
for code in [
'return document.title', 'return JSON.stringify(performance.timing)'
]:
print(self.driver.execute_script(code))
# print(self.driver.execute_script('return document.title;return JSON.stringify(performance.timing)'))
# JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。
# performance.timing 加载和使用当前页面期间发生的各种事件的性能计时信息。

<span ><span >def</span> <span >test_modify_traindate</span>(<span >self</span>):</span>
    <span ># 12306时间选择框演示</span>
    self.driver.get(<span >'https://www.12306.cn/index/'</span>)
    sleep(<span >2</span>)
    <span ># 通过js代码设置时间(需先去除readonly属性)</span>
    self.driver.execute_script(
        <span >'a=document.getElementById("train_date");a.removeAttribute("readonly");a.value="2020-12-31"'</span>)
    sleep(<span >2</span>)
    print(self.driver.execute_script(<span >'return document.getElementById("train_date").value'</span>))

 

常用JavaScript脚本

driver.execute_script("document.body.style.zoom='0.5'") #网页缩小
driver.execute_script("document.body.style.zoom='1.7'") #网页放大
driver.executeScript("window.scrollTo(window.pageXOffset,document.body.scrollHeight)"); #滑动到页面最底部
driver.executeScript("window.scrollTo(window.pageXOffset, 0)");#滑动到页面最顶部
driver.executeScript("arguments[0].scrollIntoView(false);", driver.findElement(by));#上下滑动使元素和窗口底部对齐
driver.executeScript("arguments[0].scrollIntoView(true);", driver.findElement(by));  #上下滑动使元素和窗口顶部对齐
driver.executeScript("window.scrollTo(document.body.scrollWidth, window.pageYOffset)") #水平滑动到页面最右边