Call JavaScript code
First, the method call JavaScript code
Selenium in the browser will be operating in an unstable part of the automation code, often wrong part, these codes can be part of the page elements to operate into the corresponding JavaScript script, because the browser supports native JavaScript, JavaScript Code performed directly in the browser kernel, the element is not clicked location error does not occur, can greatly improve the stability and efficiency of the implementation of automation use cases.
JavaScript calls in two ways:
1, execute_script () : This is a synchronous method, use it to execute js code will block the main thread execution until js code is completed.
execute_script () method if the return value, there will be the following situations:
-
- Return to a page element (document element), this method returns a WebElement
- Returns the floating-point numbers, this method returns a double type of digital
- Returns non-floating-point numbers, the method returns a long type digital
- Blloean return type, the method returns a Boolean
- It returns an array of methods to play a List type
- Otherwise, it returns a string
2, execute_async_script () : This is an asynchronous method, it will not block the main thread.
Two, execute_script () and execute_async_script () difference
JavaScript execution differences synchronous and asynchronous execution of JavaScript
Synchronous execution: driver.execute_script (js)
If the execution time is shorter JavaScript code, you can choose to synchronize execution because Webdriver will wait for the results of the implementation of sync, and then run other code.
Asynchronous execution: driver.execute_async_script (js)
If the JavaScript code execution time is longer, you can choose asynchronously, because Webdriver not wait for the results of its execution, but directly execute the following code.
Third, give chestnuts
Let's take a practical look at how to use chestnuts, execute_script () retrieved execute JavaScript code. Simple call alert js elastic block statement, specific code as follows:
from selenium.webdriver Import Chrome from Time Import SLEEP # open Baidu Home Driver = Chrome ( " C: \ Program Files (x86) \ Google \ Chrome \ the Application \ chromedriver.exe " ) driver.get ( " HTTPS: // the WWW. baidu.com/ " ) # eject a projectile alert box JS = " alert ( 'which is a bomb alert box'); " driver.execute_script (JS) SLEEP ( 2 ) # close the popup box driver.switch_to.alert.accept ( ) SLEEP ( 2 ) driver.quit ()
Let's look at a chestnut. As we all know, used to adjust the position of the scrollbar of the browser JavaScript code is as follows: window.scrollTo (0,450);
the window.scrollTo () method is used to set horizontal and vertical position of the scroll bar of the browser window. The first argument of the left represents the pitch level of the second parameter on the vertical margins. Code is as follows:
from selenium.webdriver Import Chrome from Time Import SLEEP # access Baidu Driver = Chrome ( " C: \ Program Files (x86) \ Google \ Chrome \ the Application \ chromedriver.exe " ) driver.get ( " http://www.baidu .com " ) # set the browser window size driver.set_window_size (500, 500 ) # Search driver.find_element_by_id ( " kw " ) .send_keys ( " the Selenium " ) SLEEP ( 2 ) # scroll through the javascript settings of the browser window position js = "window.scrollTo(100,450);" driver.execute_script(js) sleep(3) driver.quit()
Open Baidu search browser, and advance through set_window_size () method of the browser window is set to a fixed format display, to let window horizontal and vertical scroll bars. Then pass
Execute_script over () method executes JavaScripts on the position to move the scroll bar.
JavaScript in WebDriver can also achieve a lot of features, we'll summarize the process of practice in the late