美文网首页
python+selenium常用的js操作

python+selenium常用的js操作

作者: _karen | 来源:发表于2021-03-17 17:38 被阅读0次

    常用的js脚本:

    • 获取页面的标题document.title
    • 页面弹框window.alert("页面弹框")
    • 获取当前页面的性能JSON.stringify(performance.timing)
    • 滑动页面到底部:document.documentElement.scrollTop=10000
    • 滑动页面到顶部:document.documentElement.scrollTop=0
    • 通过js去定位元素:document.getElementById('el-icon-git-pause')
    • 操作时间控件

    执行这些js脚本:
    1.execute_script 执行js 2.return 可以返回js执行后的结果 3.传参:使用arguments进行传参
    关于arguments可以查看这篇文章:https://www.cnblogs.com/gongyijie/p/9425549.html
    比如执行获取页面的标题脚本:
    self.driver.execute_script('document.title')
    获取页面的标题并返回:
    title= self.driver.execute_script('return document.title')
    通过id去获取一个元素并且点击click()

    ele = self.driver.execute_script('document.getElementById('el-icon-git-pause')')  
    ele.click()
    

    或者直接一句解决:
    self.driver.execute_script("document.getElementById('su').click")

    操作时间控件

    三个脚本写在一起,用分号隔开
    self.driver.execute_script("ele = document.getElementById('su');ele.removeAttribute('readonly');ele.value = '2021-10-01'")

    相关文章

      网友评论

          本文标题:python+selenium常用的js操作

          本文链接:https://www.haomeiwen.com/subject/ourecltx.html