Seleniem---Python3---JS调用

'''
使用execute_script(script,*args)方法来执行js代码

    执行js一般有两种场景:
         1;在页面上直接执行JS
         2;在某个已经定位元素上执行JS
'''
        
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>js</title>
<script type="text/javascript" async=""
src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function(){
$('#tooltip').tooltip({"placement": "right"});
});
</script>
</head>
<body>
<h3>js</h3>
<div class="row-fluid">
<div class="span6 well">
<a id="tooltip" href="#" data-toggle="tooltip" title=" selenium-webdriver(python)">hover to see
tooltip</a>
<a class="btn">Button</a>
</div>
</div>
</body>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>
#coding = utf-8

from selenium import webdriver
import os,time

#Selenium 下拉框处理

#D:\chromedriver_win32\chromedriver.exe

class driver_js(object):
    def __init__(self):
        self.driver = webdriver.Chrome('D:\chromedriver_win32\chromedriver.exe')

    def dirver_js1(self):
        file_path = 'file:///' + os.path.abspath('js.html')
        self.driver.get(file_path)

        #通过JS隐藏选中的元素
        #隐藏文字信息
        self.driver.execute_script('$("#tooltip").fadeOut();')
        time.sleep(5)

        #隐藏按钮
        button = self.driver.find_element_by_class_name('btn')
        self.driver.execute_script('$(arguments[0]).fadeOut()',button)
        time.sleep(5)

        self.driver.close()



if __name__ == "__main__":
    a = driver_js()
    a.dirver_js1()

 

posted @ 2020-04-17 13:44  天象独行  阅读(174)  评论(0编辑  收藏  举报