Tutorial 2 www.MIpython.com : Random IP Browsing Script / Bot with Selenium and

Blog Forums MICHIGAN PYTHON COMMUNITY Tutorial 2 www.MIpython.com : Random IP Browsing Script / Bot with Selenium and

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #96
    MIPYTHON
    Keymaster

    Today we are making a script to enter user input into a field on a webpage. Locating the element with Selenium and programmatically sending various key strokes. We are going to navigate from the command line to an instance of Google Chrome. Open up the site https://www.kproxy.com/ . We will take user input from the command line and pass it to submit field. Then click the submit button to surf an IP anonymously.

    UPDATE:
    This script loads one instance of GOOGLE Chrome then asks for another completely new instance of Chrome for a new url infinatum. Each browser instance has(should, checked and verified so far) its own random IP from kproxy.com

    We should all know how to get here at this point. So, I assume you have Python set up and your libraries installed correctly.

    Open up you IDE ( I often just use notepad++)

    Lets make a new file called selenium_anon_input.py

    First we need to import our modules. Just Selenium for now.
    [code]
    ###### IMPORT SELENIUM
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    [/code]

    Lets define a function called bot1 to do our work for us. This bot is going to just grab an URL for now and open it into a new instance of Google Chrome. As you can see below we first define(def) the function name(bot1) with the Pythonic syntax and indentation.

    Ask for user input for the URL to send through the Anonimyzer

    Next, we pass the webdriver to variable driver.

    [code]
    def bot1():
    ############## USER INPUT URL IN COMMAND LINE
    url = input(“WHAT URL WOULD YOU LIKE TO VISIT ANON? http://”)
    ############### CHROME WEBDRIVER
    driver = webdriver.Chrome()
    ############### GET URL FOR DRIVER VAR
    driver.get(“https://www.kproxy.com/”)
    [/code]

    Lets run our script and see what happens.
    [code]
    ################ Run Bot1
    print(“INITIALIZE BOT 1”)
    bot1()
    [/code]

    At this point we should be seeing a new instance of Chrome opening up and going to http://www.kproxy.com. Our user input is not doing anything yet. Lets fix that. For this we are going to locate the elements we need on the page with Selenium:

    driver.find_element_by_id

    To get the elements id name for selenium to use. We simply need to navigate to http://www.kproxy.com with any browser. Right click “inspect element” on the text input field and submit button. Copy and paste this information into the code below.
    Code so far.
    [code]
    ###### IMPORT SELENIUM
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys

    def bot1():
    ############## USER INPUT URL IN COMMAND LINE
    url = input(“WHAT URL WOULD YOU LIKE TO VISIT ANON? http://”)

    ############### CHROME WEBDRIVER
    print(“DRIVER IS CHROME”)
    driver = webdriver.Chrome()

    ############### GET URL FOR ANONIMYZER
    print(“GETTING kproxy.com”)
    driver.get(“https://www.kproxy.com/”)

    ############### FIND ELEMENT ID MAINTEXTFIELD ####
    elem = driver.find_element_by_id(“maintextfield”)

    ############### SEND URL VAR TO MAINTEXTFIELD ###
    send_url = elem.send_keys(url)
    send_url

    [/code]

    With the above code we defined a new variable called elem and passed the driver.find_element_by_id method from Selenium to it.

    We then created a variable called send_url. That passes the user inputed url as an argument to the Selenium method .send_keys. This passes the url argument to the element id named “maintextfield”.

    Now lets click on the submit button and send the url through with the code below.
    [code]
    ############### FIND ELEMENT BY CLASS NAME BOTON
    elem = driver.find_element_by_class_name(“boton”)
    elem.click()
    [/code]
    UPDATE:
    Simplified and shortened the find and key submit method in Selenium with this function call:

    elem = driver.find_element_by_id(“maintextfield”).send_keys(Keys.ENTER)

    Easier and more compact to locate an element and send a key or variable. The original code is commented out around the update.

    Our completed script should look like this
    EDIT: added a element clear method and instead of clicking on the submit button the RETURN / ENTER key is pressed.
    also added an infinite loop that keeps the function bot1() webdriver window open. It’s kinda ghetto but it works for this instance.
    You can go back to the terminal and type in a new url after the old one loads. To exit, exit out of terminal or web browser.
    [code]
    print(“*” * 30 )
    print(“””### MI PYTHON SELENIUM RANDOM IP BROWSER BOT MK2
    # MIpython.com
    # SCRIPT TO USE SELENIUM WEBDRIVER METHODS
    # TO OPEN UP A USER INPUTED URL THROUGH
    # A WEB ANONIMYZER.
    “””)
    print(“*” * 30 )

    ###### IMPORT SELENIUM
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys

    def bot1():
    ############## USER INPUT URL IN COMMAND LINE
    url = input(“WHAT URL WOULD YOU LIKE TO VISIT ANON? http://”)

    ############### CHROME WEBDRIVER
    print(“DRIVER IS CHROME”)
    driver = webdriver.Chrome()

    ############### GET URL FOR ANONIMYZER
    print(“GETTING kproxy.com”)
    driver.get(“https://www.kproxy.com/”)

    ############### FIND ELEMENT ID MAINTEXTFIELD ####
    #elem = driver.find_element_by_id(“maintextfield”)

    ############### FIND AND CLEAR FIELD OF ELEMENT NAME MAINTEXTFIELD ####
    #print(“FINDING AND CLEARING ELEMENT”)
    elem = driver.find_element_by_id(‘maintextfield’).clear()

    ############### SEND URL VAR TO MAINTEXTFIELD ###
    elem = driver.find_element_by_id(“maintextfield”).send_keys(url)
    #send_url = elem.send_keys(url)
    #send_url
    ############### RETURN KEY PRESS
    elem = driver.find_element_by_id(“maintextfield”).send_keys(Keys.ENTER)
    ############### FIND ELEMENT BY CLASS NAME BOTON
    #elem = driver.find_element_by_class_name(“boton”)
    ############### CLICK ON ELEMENT
    #elem.click()

    run = True
    while run == True:
    bot1()
    else:
    print(“press ctrl-c to exit”)
    #### RUN BOT1 FUNCTION
    print(“RUN BOT1”)
    bot1()
    [/code]

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.