This article is participating in Python Theme Month. See the link for details

If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.

Selenium has some advanced techniques beyond the basics of web pages, which will be illustrated in the next two articles.

Selenium slider operation

Slider verification is a kind of evolution of verification code, in 2020 has not often seen, most have been replaced by sliding jigsaw verification code, but there are a few websites, still use sliding verification, such as Tmall registration page, Ctrip registration page.

This blog takes Tmall registration as an example to explain how to solve the problem of slider verification code for you.

The steps are as follows:

  1. Open the Tmall registration page;
  2. Agree to the Registration Agreement;
  3. Drag the slider to the far right.

Open the registration page and click Agree

Open the page and take a look at the Agreement button, focusing on how to obtain the button element.

After executing code, find problems, open Tmall registration page source of https://register.tmall.com/, which was not to get a label, a web page, but nested ~

After checking, the real registered address was found to be as follows:

https://reg.taobao.com/member/reg/fill_mobile.htm
Copy the code

With the real address, you can write the code.

from selenium import webdriver

import time
driver = webdriver.Firefox()
# browser maximization
driver.maximize_window()
# Open the registration page
driver.get('https://reg.taobao.com/member/reg/fill_mobile.htm')

# Click the Agree button J_AgreementBtn
driver.find_element_by_id("J_AgreementBtn").click()

time.sleep(5)
driver.quit()
Copy the code

Find the slider area with the slider

Click on the Agreement to find the slider, using the developer tools to locate it.

Finally, after several attempts, the resulting code looks like this:

# Find the slider area part of the code
scale = driver.find_element_by_id("nc_1_n1t")
print(scale.size)

# Discover region slider
sc = driver.find_element_by_by_id("nc_1_n1z")
print(sc.size)
Copy the code

Implement drag link

Drag we use the drag_and_drop_to_offset method provided by Selenium. The specific code is as follows. There is fine tuning in the process of the following code, that is to say, the parameters need to be adjusted repeatedly to achieve the desired effect.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Firefox()
# browser maximization
driver.maximize_window()
# Open the registration page
driver.get('https://reg.taobao.com/member/reg/fill_mobile.htm')

# Click the Agree button J_AgreementBtn
driver.find_element_by_id("J_AgreementBtn").click()

# Find the slider area part of the code
scale = driver.find_element_by_id("nc_1_n1t")
print(scale.size)

# Discover region slider
source = driver.find_element_by_id("nc_1_n1z")
print(source.size)

# Drag the slider from left to right
ActionChains(driver).drag_and_drop_by_offset(source,scale.size["width"],source.size["height"]).perform()

time.sleep(5)
driver.quit()
Copy the code

Concrete implementation code after the effect appears, or very interesting, I hope you can run it.

Selenium implements screenshot operations

Taking screenshots through Selenium serves two purposes in automated testing. One is to capture error information to facilitate testing, and the other is to capture captcha for subsequent processing.

Web Screenshot Operation

Implementing web screenshots is very simple, directly use Selenium’s own save_screenshot method, with this method, you don’t need to install plug-ins in the page screenshots, you can write a small program.

driver.save_screenshot("dangdang.png")
Copy the code

Direct use to obtain is the browser to visit the first screen of the screen, how to achieve interception of the entire web page? It only needs to be solved through JS.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Firefox()

# Open the registration page
driver.get('http://www.dangdang.com/')

Get the width and height of the page from the JS code
width = driver.execute_script("return document.documentElement.scrollWidth")
height = driver.execute_script("return document.documentElement.scrollHeight")

Set the width and height of the browser
driver.set_window_size(width, height)

time.sleep(5)
driver.save_screenshot("dangdang.png")

driver.quit()
Copy the code

Screenshots of specified elements

The element screenshots are actually done in conjunction with another library, called Pillow, which is dedicated to processing images.

Install the new library via PIP Install Pillow.

Next to the way niu travel network registration page as an example, to show you how to intercept the specified elements. Note the following code to obtain the web element coordinates method, later found that the method is not very good, N more attempts, try out the number. Different website solution is different, can try by oneself.

from selenium import webdriver
import time
from PIL import Image
driver = webdriver.Firefox()
driver.maximize_window()
# Open the registration page
driver.get('https://passport.tuniu.com/register')
time.sleep(3)
# Capture full screen
driver.save_screenshot("./tuniu.png")
Find the verification code area identify_img
code = driver.find_element_by_id("identify_img")
# get the coordinate of the element in the page. This place is not accurate through the code
left = 1040 # code.location['x']
up = 590 #code.location['y']

print(code.size["width"],code.size["height"])

right = left + code.size["width"]
below = up + code.size["height"]

im = Image.open('./tuniu.png')
im = im.crop((left, up, right, below))
im.save("./code.png")
driver.quit()
Copy the code

After getting the verification code picture, you can automatically log in to some websites through some coding platforms.

Write in the back

This blog post covers Selenium slider operations and web page/element screenshots. After learning about Selenium, you will have improved your knowledge of Selenium.


Blogger ID: Dream eraser, hope you like, comment, favorites.