selenium调用js文件_selenium肿么调用执行这两个js函数
調用js方法
execute_script(script, *args)
在當前窗口/框架 同步執行javaScript
腳本:JavaScript的執行。
*參數:適用任何JavaScript腳本。
使用:
driver.execute_script(‘document.title’)
使快播登陸用戶名輸入框標紅顯示:
#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fvod.kuaibo.com%2F%3Ft%3Dhome")
#給用戶名的輸入框標紅
js="var q=document.getElementById(\"user_name\");q.style.border=\"1px solid red\";"
#調用js
driver.execute_script(js)
time.sleep(3)
driver.find_element_by_id("user_name").send_keys("username")
driver.find_element_by_id("user_pwd").send_keys("password")
driver.find_element_by_id("dl_an_submit").click()
time.sleep(3)
driver.quit()
js解釋:
q=document.getElementById(\"user_name\")
元素q的id 為user_name
q.style.border=\"1px solid red\
元素q的樣式,邊框為1個像素紅色
隱藏元素
js.html
js$(document).ready(function(){
$('#tooltip').tooltip({"placement": "right"});
});
js
hover to see tooltip
Button
查看本欄目更多精彩內容:http://www.bianceng.cn/Programming/extra/
(保持html文件與執行腳本在同一目錄下)
執行js一般有兩種場景:
一種是在頁面上直接執行JS
另一種是在某個已經定位的元素上執行JS
#coding=utf-8
from selenium import webdriver
import time,os
driver = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('js.html')
driver.get(file_path)
#######通過JS 隱藏選中的元素#########
#第一種方法:
driver.execute_script('$("#tooltip").fadeOut();')
time.sleep(5)
#第二種方法:
button = driver.find_element_by_class_name('btn')
driver.execute_script('$(arguments[0]).fadeOut()',button)
time.sleep(5)
driver.quit()
js解釋:
arguments對象,它是調用對象的一個特殊屬性,用來引用Arguments對象。Arugments對象就像數組。
fadeOut() 方法使用淡出效果來隱藏被選元素,假如該元素是隱藏的。
取消
評論
總結
以上是生活随笔為你收集整理的selenium调用js文件_selenium肿么调用执行这两个js函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python异步和进程_Python异步
- 下一篇: docker harbor 域名_超详细