Validation of selenium + chrome driver common slider

target

  • Using selenium to realize automatic login
  • Tidy up your ideas
    Get the ID of the password input box of the account - > fill in the account - > fill in the password - > appear a slider - > slide over the slider - > Click login

process

  • The process of getting the input box id is not introduced any more, but the element of'iframe', for example:

    <iframe src="" frameborder="0" id="iframe_id">
    <html>
    ...
    <input type="text" id="user_name">
    <input type="text" id="password">
    <button id="login">Sign in</button>
    ...
    </html>
    </iframe>
    

    The data in the iframe tag can not be obtained directly, so it needs to be switched once to get the data.

    # Switch to iframe
    driver.switch_to_frame("iframe_id")
    # Clear the account box
    driver.find_element_by_id("user_name").clear()
    # Fill in account number
    driver.find_element_by_id("user_name").send_keys(username)
    # Fill in the password
    driver.find_element_by_id("TPL_username_1").clear().send_keys(password)
    
  • When filling in the password, there is a slider verification. First, don't rush to make the slider, sleep for a while, then manually pull the slider, surprise comes.

  • Maybe the technology of a treasure is relatively high-end. No matter how you pull it, it will fail. It's amazing. The reason is that the current page is opened by selenium by using some js method. So it's considered a crawler. It's impossible to let you go.

Solution

  • You need to modify the contents of the chromedriver file

    Change'cdc_asdjflasutopfhvcZLmcfl'to any character with the same number of characters, and execute in root mode, otherwise the file will be damaged, remember to backup.
    For details, please refer to: stackoverflow-selenium-with-chromedriver

  • Problem solving, start automatic slider operation, normal to get the slider id to operate, but I want to use a strange way.

    # Slide verification
    from selenium.webdriver.common.action_chains import ActionChains
    
    # Slider validation, assuming that the id of the slider is not available
    def the_slider_validation(driver):
        builder = ActionChains(driver)
        # Get the button to log in
        l = driver.find_element_by_id("login")
        # Clear up the previous action
        builder.reset_actions()
        # Calculate the trajectory of the slider movement, 300 is the moving distance.
        track = move_mouse(300)
        # Move to the 35-pixel position on the login button (where the slider is), and locate the slider according to the login button
        builder.move_to_element_with_offset(l, 0, -35)
        # Click on the left button, do not release
        builder.click_and_hold()
        time.sleep(0.2)
        # Start to generate trajectories
        for i in track:
            builder.move_by_offset(xoffset=i, yoffset=0)
            builder.reset_actions()
        time.sleep(0.1)
        # Release the left key to perform the action in for
        builder.release().perform()
    
    # Mouse movement
    def move_mouse(distance):
        remaining_dist = distance
        moves = []
        a = 0
        # Acceleration, faster and faster.
        while remaining_dist > 0:
            span = random.randint(15, 20)
            a += span
            moves.append(a)
            remaining_dist -= span
            if sum(moves[:-1]) > 300:
                print(sum(moves))
                break
        return moves
    
  • Verify that it passes, click login, or fail to retry

    driver.find_element_by_id("login").click()
    

Links to this article: Time No Blog - selenium + chrome driver common slider verification

Tags: Selenium

Posted on Thu, 10 Oct 2019 03:22:13 -0700 by skylark2