Python + Selenium: How to Use? [SE03]
by - Thursday, January 1, 1970 at 12:00 AM
Bro, how do you feel after warming up?
Trust me, it's simple. When you are familiar with all the letters + numbers on the keyboard, then you can master Python completely. If you run into problems, don't give up!
Because I can tell you responsibly, every programmer climbs out of the mine, and sometimes even a problem leads to thinking for a few days. Relatively speaking, our progress is very fast.

In this section, let's start the actual combat.
Warning: If you use the code in the example to do something illegal, it's none of my business.

In this section, we use a real commercial site for testing.
https://mejuri.com/
If this admin sees this post, I'm sure he can forgive me for this behavior, because on the site architecture, single sign-on can easily cause stress concentration.

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 2022-07-28
# Breached.to Bullet Class:
# THE FAST ONLINE VERSION 1.0CB

# Import selenium package
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

if __name__ == '__main__':
    """
    Create a selenium object, then you can use the methods of this object, if you don't understand, you can ignore it, copy and paste with me
    """
    # Custom option parameters
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("excludeSwitches", ["enable-logging"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument('--disable-blink-features=AutomationControlled')
    options.add_argument('disable-cache')
    options.add_argument('-ignore-certificate-errors')
    options.add_argument('-ignore -ssl-errors')

    # Download the Chrome kernel locally, then, we load it
    _chromePath = r"r:\chromedriver.exe"
    # Use Service(), initialize
    chromeService = Service(_chromePath)
    # The only thing to note about the two parameters here is options. In the front, we customized these options, then DRIVER will use it as we customized
    DRIVER = webdriver.Chrome(options=options, service=chromeService)

    # Real combat code:
    BASE_URL = "https://mejuri.com/"
    DRIVER.get(BASE_URL)

    # Changes from previous: different places
    success_Box_Title_Str = ['Mejuri | Everyday Fine Jewelry | Online Jewelry Shop', ]
    success_Box_URL_Str = ["https://mejuri.com/", ]
    if DRIVER.title in success_Box_Title_Str or DRIVER.current_url in success_Box_URL_Str:
        print("Debug Output: Specific business code part...")
    else:
        print("Debug Output: Open URL Fail...")


If nothing else, you can see the debug output:
Debug Output: Specific business code part...

Well, now comes to the key position, first we will list them, and then we will solve them one by one:
1. This spaghetti code
2. Find the username and password elements and perform an input test
3. Multithreading, and then executing concurrently (for example, I want to test 100 accounts together)
4. Custom use proxy
5. Customize User-Agent
6. Load account information from a text file or my database
7. Save the results

Before each project starts, you need to calm down and sort out the client (or your own thoughts), make a strong coffee, and enjoy the process.
If you're in the gym or in your head thinking about women, making money, then, trust me, after you put in the practice, you're going to be miserable because once you forget the steps, you need to think ten times as long.Like many brothers who do website penetration testing, success does not happen by accident, it is only after many times of practice.

Back to the topic:

The 7 steps are listed above. We will now focus directly on the target and find the login part. We will skip the other steps for the time being (because this involves a lot of basic knowledge of Python)

Copy the code below, notice that there are two changes:
1. On lines 13 and 14, a new Selenium feature is introduced
# New Import
from selenium.webdriver.common.by import By


2. Added business logic code in lines 47~60
print("Debug Output: Specific business code part...")
        # 1. Look for the login element:
        _element_Login_btn = DRIVER.find_element(By.XPATH, '/html/body/div[1]/div[3]/div/section/header/nav/div[2]/button/span')
        # 2. Use the sleep method to force a delay.
        # This is an officially not recommended method, and is only used here as an explanation.
        time.sleep(1)
        # 3. Once the login element is found, click it using the click() method.
        _element_Login_btn.click()
        time.sleep(1)
        # 4. Navigate to the username input element
        _element_input_accountBox = DRIVER.find_element(By.XPATH, '//*[@id="input-email"]')
        _element_input_accountBox.send_keys("[email protected]")
        time.sleep(1)
        _element_continue_btn = DRIVER.find_element(By.XPATH, '/html/body/div[1]/div[8]/div[1]/div/div/div/div[2]/div[2]/form/div[2]/button/span')
        _element_continue_btn.click()


The following is the complete code, please refer to:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 2022-07-28
# Breached.to Bullet Class:
# THE FAST ONLINE VERSION 1.0CB

# Import selenium package
import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# New Import
from selenium.webdriver.common.by import By

if __name__ == '__main__':
    """
    Create a selenium object, then you can use the methods of this object, if you don't understand, you can ignore it, copy and paste with me
    """
    # Custom option parameters
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("excludeSwitches", ["enable-logging"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument('--disable-blink-features=AutomationControlled')
    options.add_argument('disable-cache')
    options.add_argument('-ignore-certificate-errors')
    options.add_argument('-ignore -ssl-errors')

    # Download the Chrome kernel locally, then, we load it
    _chromePath = r"r:\chromedriver.exe"
    # Use Service(), initialize
    chromeService = Service(_chromePath)
    # The only thing to note about the two parameters here is options. In the front, we customized these options, then DRIVER will use it as we customized
    DRIVER = webdriver.Chrome(options=options, service=chromeService)

    # Real combat code:
    BASE_URL = "https://mejuri.com/"
    DRIVER.get(BASE_URL)

    # Changes from previous: different places
    success_Box_Title_Str = ['Mejuri | Everyday Fine Jewelry | Online Jewelry Shop', ]
    success_Box_URL_Str = ["https://mejuri.com/", ]
    if DRIVER.title in success_Box_Title_Str or DRIVER.current_url in success_Box_URL_Str:
        print("Debug Output: Specific business code part...")
        # 1. Look for the login element:
        _element_Login_btn = DRIVER.find_element(By.XPATH, '/html/body/div[1]/div[3]/div/section/header/nav/div[2]/button/span')
        # 2. Use the sleep method to force a delay.
        # This is an officially not recommended method, and is only used here as an explanation.
        time.sleep(1)
        # 3. Once the login element is found, click it using the click() method.
        _element_Login_btn.click()
        time.sleep(1)
        # 4. Navigate to the username input element
        _element_input_accountBox = DRIVER.find_element(By.XPATH, '//*[@id="input-email"]')
        _element_input_accountBox.send_keys("[email protected]")
        time.sleep(1)
        _element_continue_btn = DRIVER.find_element(By.XPATH, '/html/body/div[1]/div[8]/div[1]/div/div/div/div[2]/div[2]/form/div[2]/button/span')
        _element_continue_btn.click()

    else:
        print("Debug Output: Open URL Fail...")


If nothing else, you should now see that the browser kernel you control automatically fills in the account number as you wish, and clicks the Continue button.
If you look closely, you will find that we have added methods in this section: find_element, click, time.sleep, send_keys.
Before starting your next class, you need to familiarize yourself with these frequently used methods.
Yes, it's still the saying that is often said, very simple, don't be fooled by these codes, the computer is very low-level, you have to learn to master it, and once you master it, it will work according to your instructions.
Reply
Thanks looking towards this Python code.
Reply
Thank you for sharing
Reply
I didn't see any obvious mistaks. You might need to read the selenium hand book because they update a lot of new stuff on version 3.0. Some of the old syntax may not be working
Reply
wow thanks
Reply
id be interested
Reply


 Users viewing this thread: Python + Selenium: How to Use? [SE03]: No users currently viewing.