This is my eighth day of the August Genwen Challenge

preface

The simulated login operation of B station is realized by Selenium. Without further ado, let’s begin happily

Results demonstrate

The development tools

Python version: 3.6.4

Related modules:

The selenium module;

And some modules that come with Python.

Chromedriver: Download the driver that matches the Chrome version on your computer from the link below:

http://npm.taobao.org/mirrors/chromedriver/
Copy the code

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Introduction of the principle

First, let’s instantiate a WebDriver.Chrome object to automate the operation of Chrome on our computer:

browser = webdriver.Chrome(executable_path=chromedriverpath, options=chrome_opts)
Copy the code

Next, we use it to automatically access the login interface of station B:

browser.get('https://passport.bilibili.com/login')
Copy the code

And automatically fill in the username and password:

driver_wait = WebDriverWait(browser, 30)
username_sender = driver_wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="login-username"]')))
username_sender.send_keys(username)
password_sender = driver_wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="login-passwd"]')))
password_sender.send_keys(password)
Copy the code

The input fields of user name and password are directly located by xpath

Sleep (3) is added to ensure that the slider verification code pops up smoothly:

button = driver_wait.until(EC.presence_of_element_located((By.XPATH, '//a[@class="btn btn-login"]')))
button.click()
time.sleep(3)
Copy the code

The slider verification code for Station B looks something like this:

You just need to drag the slider to the corresponding notch position, the difficulty of the problem is how to determine the position of the notch. The simplest and most effective way is to directly compare the original captcha image with the image with the notch to get the position of the notch. For station B, you can do this. First, get the original captcha image:

image_ori = browser.execute_script('return document.getElementsByClassName("geetest_canvas_fullbg")[0].toDataURL("image/png"); ')
image_ori = image_ori.split(', ') [1]
image_ori = base64.b64decode(image_ori)
image_ori = Image.open(io.BytesIO(image_ori))
Copy the code

Similarly, get a graph of captcha notched:

image_gap = browser.execute_script('return document.getElementsByClassName("geetest_canvas_bg")[0].toDataURL("image/png"); ')
image_gap = image_gap.split(', ') [1]
image_gap = base64.b64decode(image_gap)
image_gap = Image.open(io.BytesIO(image_gap))
Copy the code

Then compare the pixel matrix of the two images to obtain notch coordinates:

gap_pos = []
for i in range(image_ori.size[0) :if gap_pos:
    break
  for j in range(image_ori.size[1]):
    pixel_ori = image_ori.getpixel((i, j))
    pixel_gap = image_gap.getpixel((i, j))
    if abs(pixel_ori[0] - pixel_gap[0) >10 and abs(pixel_ori[1] - pixel_gap[1) >10 and abs(pixel_ori[2] - pixel_gap[2) >10:
      gap_pos = [i, j]
      break
Copy the code

Once we have the coordinates of the notch, all we need to do is move to the position of the notch using the Browser control slider:

slider = driver_wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div[6]/div/div[1]/div[2]/div[2]')))
ActionChains(browser).click_and_hold(on_element=slider).perform()
tracks = TrackGenerator.getTracksByExpfunc(distance * 0.93)
for delta_dis in tracks:
  ActionChains(browser).move_by_offset(xoffset=delta_dis, yoffset=0).perform()
ActionChains(browser).pause(0.5).release().perform() We can use an exponential function to simulate the movement trajectory of the slider:def getTracksByExpfunc(distance, delay=5) :
  tracks = []
  offset = 0
  for i in np.arange(0.1, delay, 0.1):
    delta_dis = round((1 - pow(2, -10 * i / delay)) * distance) - offset
    tracks.append(delta_dis)
    offset += delta_dis
  tracks[-1] += (distance - offset)
  return tracks
Copy the code

This is the end of the article, thanks for watching, Python information security, next article will share Python+Selenium decoding 12306 captchas

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

⑤ A Roadmap for Learning Python

All done~ See personal homepage introduction to obtain the complete source code.

Review past

Simple implementation of entry level steganography

Using Python+Selenium to crack spring Aviation network slider verification code, information security road