SoFunction
Updated on 2024-11-12

Python implementation of the slider CAPTCHA details

This section is going to explain the slider CAPTCHA shown below (the more complex sliding puzzle CAPTCHA is shown in thenext article(Introduction). This CAPTCHA mechanism is relatively simple: drag the slider to the rightmost end of the slide to complete the validation, as shown below. If you don't drag the slider to the right-most end of the slide, it won't pass the validation, and the slider will return to its starting position after the validation fails.

The key to this is that you need to use the Selenium library to simulate the mouse dragging the slider to slide a certain distance. Because the starting position of the slider is the same as the starting position of the slide, the distance the slider needs to move is equal to the span of the slide minus the width of the slider. Here's how to view the width of the slide and slider using the developer tools.

Open the local web page file "" built for the slider CAPTCHA in the companion code file of this book in the browser, open the developer tools, and then use the element selection button to select the entire slider, at this time the interface is shown in the following figure. You can see which shows the slider size and color and other properties. To view the size of the slide, there are two ways: shown by the arrow in the figure.

The width of the slider is viewed in the same way as the slide, by selecting the slider with the element selection tool. This shows that the distance that needs to be simulated for the slide is 260 pixels.

Below is a picture to start writing the code. First open the web page with Selenium library with the following code:

from selenium import webdriver
browser = ()
url = r'D:\works\python_crawl1\"Python Crawler (Advanced and Progressive)" code summary\2.CAPTCHA backcrawl\3.Slider CAPTCHA\'
(url) #Open a web page with an emulated browser

Then use Selenium to position the slider with the following code:

huakuai = browser.find_element_by_xpath('//*[@]/span')

Once you have positioned the slider, you are ready to drag it. In the process of dragging to keep the mouse for the state of the mouse is pressed, can not be too early to release the mouse, so you can not use the click () function. Selenium library provides an ActionChains module, which click_and_hold () function can make the mouse to keep the state of the mouse is pressed, release () function can release the mouse, move_by_ offset() function to move the mouse. Combined use of these functions can drag the slider a certain distance, the code is as follows:

action = (browser) # Initiate the action chain
action.click_and_hold(huakuai).perform() # Press and hold the slider
action.move_by_offset(260,0) # move the slider, where 260 is the previously calculated distance to be slid
().perform() # Release the slider

The complete code is as follows, which also uses the time library's sleep() function to wait two seconds before simulating a slide in order to observe the sliding effect.

from selenium import webdriver
import time
# 1. Access to the Web site
browser = ()
url = r'D:\works\python_crawl1\"Python Crawler (Advanced and Progressive)" code summary\2.CAPTCHA backcrawl\3.Slider CAPTCHA\'
(url) #Open a web page with an emulated browser
# 2. Positioning slider
huakuai = browser.find_element_by_xpath('//*[@]/span')
# 3. Start sliding
action = (browser) # Initiate the action chain
action.click_and_hold(huakuai).perform() # Press and hold the slider
(2)
action.move_by_offset(260,0) # move the slider, where 260 is the previously calculated distance to be slid
().perform() # Release the slider

The final run result is shown below, simulating the slider successfully.

It should be noted that now there are some web pages containing slider CAPTCHA will detect whether the current browser is the Selenium library webdriver simulation browser, if so, it will be difficult to simulate the slide successfully. This kind of anti-climbing mechanism is not CAPTCHA anti-climbing, but webdriver anti-climbing, which is difficult to deal with. Here is a clever solution: if you need to slide verification in the login phase (such as Taobao login), you can use () in the code to wait for a period of time, and during this period of time to manually login in other ways, such as manually scanning the code to login, and then continue to use the Selenium library to crawl after the login is successful.

This article on the Python implementation of the slider CAPTCHA detailed article is introduced to this, more related Python slider CAPTCHA content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!