selenium Webdriver自动化测试之执行JavaScript脚本

news/2024/7/10 1:42:42 标签: selenium, javascript, java, vue, 软件测试

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

 具体DOM操作使用方法见之前写的一篇博文:https://www.cnblogs.com/feng0815/p/8215768.html

关于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)

    def teardown_class(self):
        self.driver.quit()
View Code

上面代码为演示代码中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  加载和使用当前页面期间发生的各种事件的性能计时信息。

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

end


http://www.niftyadmin.cn/n/1345869.html

相关文章

HTTPS的单向与双向认证

HTTPS的请求相比http请求,增加了证书认证步骤: 单向认证中,需要客户端导入服务端证书即可。 使用keytool即可: keytool -import -alias 别名 -keystore cacerts -file 证书文件 -trustcacerts keytool的使用参数大致列举如下&…

Win XP远程桌面双管理员同时登录

从[url]http://www.pconline.com.cn/pcedu/soft/st/winxp/0409/other/termsrv2005.rar[/url]下载,将解压后的termsrv.dll.2055文件更改为termsrv.dll,然后重启按F8键切换到安全模式下,将其替换C:\WINDOWS\system32下的同名文件。(对于Windows…

selenium WebDriver 自动化测试之文件上传及弹框alert处理

文件上传 input标签可直接使用send_keys(文件地址)上传文件 self.driver.find_element_by_id(上传按钮id).send_keys(文件路径文件名)下面以百度图片搜索上传图片为例 #!/usr/bin/python # -*- coding: UTF-8 -*- """ author:chenshifeng file:base.py time:202…

33、VCF格式

转载:http://blog.sina.com.cn/s/blog_7110867f0101njf5.html http://www.cnblogs.com/liuhui0622/p/6246111.htmlhttp://vcftools.sourceforge.net/specs.htmlhttp://en.wikipedia.org/wiki/Variant_Call_Formathttp://blog.sina.com.cn/s/blog_74cbb8e80101f…

Ibatisnet Quick Start

准备工作1. 下载ibatis软件包(http://ibatis.apache.org/dotnetdownloads.html)。2. 创建测试数据库,并在数据库中创建一个Person 表,其中包含三个字段:Name Type SizePER_ID …

Git安装及使用以及连接GitHub方法详解

Git是目前世界上最先进的分布式版本控制系统,没有之一,对,没有之一。著名的同性交友网站-Github,使用的就是Git存储。无数的开源项目在Github上汇聚,由此可知Git的威力。 一、Git简介 Git是一个分布式的版本控制系统&a…

后台返回null iOS

1。第一种解决方案 就是在每一个 可能传回null 的地方 使用 if([object isEqual:[NSNUll null]]) 去判断 2。第二种解决方案 网上传说老外写了一个Category,叫做NullSafe..只支持到ios9,3 ,实测 并没有解决我的问题.. NullSafe的原理见 https://www.…

conftest常用hook函数

# 钩子函数,解决中文用例名称显示乱码 def pytest_collection_modifyitems(items):"""测试用例收集完成时,将收集到的item的name和nodeid的中文显示在控制台上:return:"""for item in items:item.name item.name.encode(…