This article is participating in Python Theme Month. See the link for details
This blog will learn how Selenium uses JavaScript and JQuery to manipulate page elements, as well as common mouse and keyboard events and cookies.
@[toc]
Selenium uses Javascript to manipulate elements
In Selenium, Webdriver does not allow you to manipulate certain controls on the browser, such as the scroll bar on the right. In this case, javascript is used indirectly. The other is execute_async_script, and the difference between the two is as follows:
- Execute_script is executed synchronously and takes a short time.
- Execute_async_script Executes asynchronously and takes a long time to execute.
It seems that the difference is not very obvious, the core is the difference between synchronous and asynchronous.
Take Baidu Pictures as an example. After opening the Baidu Pictures website, enter a piece of content in the Console TAB of the developer tool through Selenium.
from selenium import webdriver
# from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
# browser maximization
driver.maximize_window()
# Open Baidu Pictures
driver.get('https://image.baidu.com/')
js = "Console. log(' I'm the eraser, author of 100 crawlers ')"
driver.execute_script(js)
Copy the code
Running the code successfully outputs the following information.
The next step is to manipulate the scrollbar through JS.
from selenium import webdriver
# from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
# browser maximization
driver.maximize_window()
# Open Baidu Pictures
driver.get('https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fr=&sf=1&fmq=1462357247335_R&pv= &ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E9%AB%98%E6%B8%85%E5%8A%A8%E6%BC%AB')
js = "Window. ScrollTo (0300)"
driver.execute_script(js)
Copy the code
At this point the code core is actually testing your JS code learning ability, if you learn JS is good, that part is very easy to master.
JQuery manipulates web elements
JQuery is a class library for javascript. It is a class library for javascript. JQuery is a class library for JAVASCRIPT.
Mouse events
In automated testing, the mouse is often controlled by code. In addition to the click operation you learned earlier, the following are commonly used.
- Context_click () Right mouse click operation
- Double_click double-click
- Drag_and_drop drag
- Move_to_element hover operation
Achieve a right mouse button operation case.
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Firefox()
# browser maximization
driver.maximize_window()
# Open Baidu Pictures
driver.get('https://image.baidu.com/')
element = driver.find_element_by_id("kw")
ActionChains(driver).context_click(element).perform()
Copy the code
The running effect is shown in the figure below.
Note that the following BUG appears if the ActionChains object is instantiated incorrectly.
AttributeError: ‘FirefoxWebElement’ object has no attribute ‘w3c’
The above BUG occurs because the code is written as follows, notice the contents of **.
element = driver.find_element_by_id("kw")
ActionChains(**element**).context_click(element).perform()
Copy the code
Sends keyboard keys into the program
The main content is to let the program simulate the effect of the keyboard keys.
- Keys.BACK_SPACE
- Keys.SPACE
- Keys.TAB
- Keys.ESCAPE
- Keys.ENTER
- Key. CONTROL,” A “: Ctrl+A
- Keys.F1
- Keys.F2
When writing the code, just send the keys through the send_keys method.
Cookie operation
For automated testing, Cookie operations on websites are also very common. Selenium provides methods to add, delete, change and check cookies, as follows.
- Add_cookie (cookie_dict) Adds a cookie with a dictionary type
- Delete_all_cookies () Deletes all cookies
- Delete_cookie (cookie_name) deletes the name as
cookie_name
The Cookie - Get_cookie (cookie_name) returns the name
cookie_name
The Cookie - Get_cookies () gets all cookies
Open CSDN and check the Cookie difference before and after simulated login.
from selenium import webdriver
import time
driver = webdriver.Firefox()
# browser maximization
driver.maximize_window()
# open CSDN
driver.get('https://www.csdn.net')
Print cookies before login
for cookie in driver.get_cookies():
print(cookie)
# Wait 20 seconds and enter the password
time.sleep(20)
Print cookies before login
print("Output cookies after login")
for cookie in driver.get_cookies():
print(cookie)
Copy the code
The obvious difference was found by comparing the number of cookies without checking the content. The left is before login, and the right is after login.
In this way, cookies can be stored locally, and then local cookies can be used to simulate users.
Write in the back
In this blog post, Selenium introduces how to execute JS code and adds mouse and keyboard events. Setting the keys on the keyboard is a self-exercise. Cookies are not only used in automated testing, but are also very important in writing crawlers.
Blogger ID: Dream eraser, hope you like, comment, favorites.