This section focuses on:
Calling the JS method
Execute_script (script, *args)
Performs JavaScript synchronously in the current window/frame
Script: The execution of JavaScript.
* Parameters: Applicable to any JavaScript script.
Use:
Driver.execute_script (' Document.title ')
Make the fast-seeding login user name input box marked red display:
#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 Explanation:
Q=document.getelementbyid (\ "User_name\")
The ID of element q is user_name
Q.style.border=\ "1px solid red\
The style of element Q, with a border of 1 pixels red
Hide elements
Js.html
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/
(Keep the HTML file in the same directory as the execution script)
The implementation of JS generally has two scenarios:
One is to execute JS directly on the page
The other is to execute JS on an element that is already positioned
#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 Hides the selected element #########
#第一种方法:
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 Explanation:
The arguments object, which is a special property of the calling object, used to reference the arguments object. Arugments objects are like arrays.
The Fadeout () method uses the fade effect to hide the selected element, if the element is hidden.
PS: You can see JS can do selenium do things, but in what kind of automation in order to (or must) to JS help, I have not met. But JS can selenium complete more powerful function, this is indisputable.
In addition, has not studied JS before, so the JS code is very unfamiliar. If you have the time, I also suggest that students should supplement this knowledge. UI Automation is inseparable from front-end technology.