Selenium执行JS脚本的方法有什么,如何操作
Admin 2022-06-08 群英技术资讯 653 次浏览
JavaScript是运行在客户端(浏览器)和服务器端的脚本语言,允许将静态网页转换为交互式网页。可以通过 Python Selenium WebDriver 执行 JavaScript 语句,在Web页面中进行js交互。那么js能做的事,Selenium应该大部分也能做。WebDriver是模拟终端用户的交互,所以就不能点击不可见的元素,有时可见元素也不能点击。在这些情况下,我们就可以通过WebDriver 执行JavaScript来点击或者执行页面元素。本文将介绍如何使用 WebDriver执行 JavaScript语句。
使用execute_script() 执行 JavaScript 代码,有两种方法实现元素操作
直接使用JavaScript实现元素定位和动作执行,主要方法有:
document.getElementById document.getElementsByClassName document.getElementsByName document.getElementsByTagName document.getElementsByTagNameNS
测试示例:
python代码:
def test_baidu(self): self.driver.get("http://www.baidu.com") self.driver.execute_script('document.getElementById("kw").value = "test"') time.sleep(2) self.driver.execute_script('document.getElementById("su").click()') time.sleep(2)
在执行过程中,WebDriver 将 JavaScript 语句注入到浏览器中,然后脚本将执行。这个注入 JavaScript 有自己的名称空间,不会干扰实际网页中的 JavaScript运行。
可以先使用WebDriver获取想要操作的元素,然后使用JavaScript执行操作。
input_ele = driver.find_element_by_id("su") driver.execute_script("arguments[0].click();", input_ele)
python代码:
def test_baidu2(self): self.driver.get("http://www.baidu.com") input_ele = self.driver.find_element_by_id("kw") self.driver.execute_script("arguments[0].value = 'test';", input_ele) time.sleep(2) baidu_ele = self.driver.find_element_by_id("su") self.driver.execute_script("arguments[0].click();", baidu_ele) time.sleep(2)
可以在语句中使用多个 JavaScript动作:
username = driver.find_element_by_xpath("//*[@id='username']") password = driver.find_element_by_xpath("//*[@id='password']") driver.execute_script("arguments[0].value = 'admin';arguments[1].value = 'admin';", username, password)
可以返回JavaScript的执行结果:
driver.execute_script("return document.getElementById('kw').value") driver.execute_script("return document.title;") # 返回网页标题
在 Web自动化测试 | ActionChains、TouchAction 中介绍了TouchAction类中scroll_from_element()也可以滑动页面。
滑动到浏览器底部
document.documentElement.scrollTop=10000 window.scrollTo(0, document.body.scrollHeight)
滑动到浏览器顶部
document.documentElement.scrollTop=0 window.scrollTo(document.body.scrollHeight,0)
更改元素属性
大部分时间控件都是 readonly属性,需要手动去选择对应的时间。自动化测试中,可以使用JavaScript代码取消readonly属性。
测试页面: https://www.12306.cn/index/
测试步骤:
python测试代码:
def test_datettime(self): self.driver.get("https://www.12306.cn/index/") # 取消readonly属性 self.driver.execute_script("dat=document.getElementById('train_date'); dat.removeAttribute('readonly')") self.driver.execute_script("document.getElementById('train_date').value='2020-10-01'") time.sleep(3) now_time = self.driver.execute_script("return document.getElementById('train_date').value") assert '2020-10-01' == now_time
Selenium WebDriver 执行 JavaScript代码是一个非常强大的功能,可以实现WebElement 接口所有功能,甚至更多的功能。比如在web性能测试中可以调用Web API接口window.performance来测试Web性能。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家详细介绍了JS实现拖拽进度条改变元素透明度,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
目录vue弹窗关闭后刷新效果问题解决关闭或刷新vue文件弹出提示框vue弹窗关闭后刷新效果问题列表点击进入详情时弹窗内容重新赋值,但是修改后未点击保存再打开同一个弹窗,数据是关闭前的样子。解决在双击时间里定义一个时间戳,每次点击传过去的值,在弹窗watch里接收时间戳再赋值。关闭页面清空对象。列表页:this.time
这篇文章主要为大家详细介绍了JavaScript编写猜拳游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要讲解JavaScript严格模式不支持八进制的问题,本文围绕JavaScript严格模式展开内容,详细介绍为什么JavaScript严格模式不支持八进制,下面来看看详细介绍,需要的朋友可以参考一下
这篇文章主要为大家介绍了JS class语法糖的深入剖析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008