阅读 85

selenium中常用js操作

 

 

开胃菜,当html元素没有id,name等这些属性信息时如何获取值呢?

通过ss选择器

$(" #test ").val()
$(" input[ name=‘test‘ ] ").val()
$(" input[ type=‘text‘ ] ").val()
$(" input[ type=‘text‘ ]").attr("value")


1.document.title获取当前页面的title
2.JSON.stringify(performance.timing)获取当前页面的性能数据
3.使用js定位元素
document.getElementById(“kw”)
document.getElementsByName("")
document.getElementsByClassName("")
document.getElementsByTagName("")
4.页面滚动
滚动到顶部
document.documentElement.scrollTop=0
滚动到底部
document.documentElement.scrollTop=10000
5.修改readonly属性
a=document.getElementById(“train_date”)
?
a.removeAttribute(‘readonly’)
undefined
a.value=‘2020-03-06’
“2020-03-06”


调用JS语法

调用JS语法需要用到selenium中的方法 execute_script() ,参数直接填写JS语法。

源码:

def execute_script(self, script, *args):
        """
        Synchronously Executes JavaScript in the current window/frame.

        :Args:
         - script: The JavaScript to execute.
         - \*args: Any applicable arguments for your JavaScript.

        :Usage:
            driver.execute_script(‘return document.title;‘)
        """
        converted_args = list(args)
        command = None
        if self.w3c:
            command = Command.W3C_EXECUTE_SCRIPT
        else:
            command = Command.EXECUTE_SCRIPT

        return self.execute(command, {
            script: script,
            args: converted_args})[value]

JS定位元素

# 1、通过元素id属性,获取元素
document.getElementById(id);

# 2、通过元素name属性,获取元素列表
document.getElementsByName(name);

# 3、通过标签名,获取元素列表
document.getElementsByTagName(tag_name);

# 4、通过类名,获取元素列表
document.getElementsByClassName("class_name");

# 5、通过选择器,获取一个元素
document.querySelector("css selector")

# 6、通过CSS选择器,获取元素列表
document.querySelectorAll("css selector")

JS修改元素属性

web自动化主要是对页面元素进行操作,有时候无法定位或者找不到元素,我们可以通过JS进行修改元素属性,然后在进行通过JS定位

# 修改元素属性
document.getElementById("id").setAttribute("属性名","属性值")

其他通过JS操作元素操作

1、删除属性

# 删除属性
document.getElementById("id").removeAttribute("属性值")

2、获取属性

# 获取元素属性值
document.getElementById("id").getAttribute(属性值)

# 如果想要获取js返回的属性值,需要js前加return
js2 = return  document.getElementById("anjing").getAttribute("name")
x = driver.execute_script(js2)
print(x)

3、修改属性值

# 修改属性值
document.getELementById("id").属性="属性值"

 

 

 

 

 

 

常用操作举例:

#鼠标滚动到底部操作
element=self.driver.execute_script("return document.getElementById(‘su‘)")
self.driver.execute_script("document.documentElement.scrollTop=10000")
#返回js元素的value值
self.driver.execute_script("return document.getElementById(‘train_date‘).value")
#修改time_element的值,并返回
self.driver.execute_script("a=document.getElementById(‘train_date‘);a.removeAttribute(‘readonly‘)")
self.driver.execute_script("a=document.getElementById(‘train_date‘).value=‘2020-03-06‘")
print(self.driver.execute_script("return document.getElementById(‘train_date‘).value"))

 

 



原文:https://www.cnblogs.com/Jungle1219/p/14886534.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐