python, selenium, ubot, tutorial

Python Selenium Meets UBot Studio X Basic Tutorial Using the Chrome Browser

Welcome Python to UBot Studio X !!

This is a BASIC Python Selenium tutorial using a free Python Plugin that enables you to use the Python Programming language inside UBot Studio

download define tutorial, ubot studio stealth, ubot studio

Download The Tutorial

Selenium is an testing suite to help you do automated testing. UBot is similar and could be used the same way. In this tutorial we are going to focus on using Python inside of UBot to do browser automation with the Chrome Browser. This guide assumes you have installed Python on your machine, if not you should see my previous guide that shows how to do just this. Hopefully, you have installed the selenium package and all went well. Selenium is an easy install with “pip install -U selenium” to update/install. If you don’t wish to update then take -U out(piip install selenium). If you didn’t add the python.exe to your windows PATH then you will need to “cd” into the directory and type python -m pip install selenium.

There is a Resources Tab in the UBot source code file(.ubot) were you can locate many of the items mentioned in this tutorial. Additionally, they will be made available here and on the Resources Page on this site.

ubot studio x resource tab, python, selenium, tutorial

Resource Tab

Requirements:

First Tab – “Hello UBotter”

python, ubot studio

This tab is to insure you have everything installed correctly and the path to your python.exe is correct as well. In the “Functions Tab” is where I made a function to return the python path for all the nodes used in this guide. This makes it easier to update the one node rather 4-5 nodes. So, please make sure you add your path to the python.exe there.If everything is hooked up well you should now be able to run the code in all tabs.

ubot function

Back to the first tab you should be able to run the nodes and get an alert pop up saying “Hello UBotter!” if not then you prolly should figure that out before continuing. If you have any questions my contact info is all over the place. Or you can ask in the comments below which would be the preferred method.

Second Tab – “chromedriver”

Now that we know Python is working we now test that the chromedriver.exe is in the correct place and run a simple python selenium script.

To use relative paths in UBot you must import the “os” module and change the “current working directory” this way Python knows where the UBot file is and more importantly the python script location.  Here I used r’$special folder’ to put the path into a variable “bot_directory” then run the os.chdir(bot_directory) python function. Great!! python knows where we are too.

In the same directory as your .ubot file is a directory called webdrivers with yep! You guessed it… the chromedriver.exe lives.

wd_path = r’webdrivers\chromedriver.exe’

The r is from the regex module, it just means to take the text literally. Python does not like \ with out it, otherwise you would need to make them / or escape them like \\. The single \ is to continue to the next line in an python script.

Anyway, that line tells selenium where the webdriver is and then we use the variable in the next line.

chrome = webdriver.Chrome(executable_path=wd_path)

This is where we create an chrome browser object that we can work with when the page loads and manipulate the DOM. “executable_path” is a keyword argument(kwaarg) that passes the webdriver(chromedriver) path to the Chrome Class.

In the next line we get to finally go somewhere… voooom!!

chrome.get(“http://ubotdocta.com/“)

This line should be pretty obvious, however in your python IDE you will notice after you type “chrome.” you will get a dropdown that shows all the available methods for the “chrome object”. What that means is prettu much all you would want to do is write there at your finger tips. We will get more into those in future tutorials. Right now we are using “get” to navigate to the site inside where we are passing the parameters(“”) the first positional argument is for the “url” which is a string.

The next line I have a sleep() from the time module(time.sleep), when you import the time like:

from time import sleep

You are not importing the entire time module. You are just bringing into memory the sleep module and there is less typing too. The sleep() method is a just like the UBot “wait command”, it is just a delay. I put it there so you can see briefly what happened then the next line closes the browser window.

chrome.close() — closes the window and

chrome.quit() — shuts down the webdriver

That was the first node chrome_1, next we will get some output using the print() function.

As you know $Execute from code is a UBot function. This will return info from the python code inside into the variable(#chromedriver_2). In this case will be what’s in the Python print() function.
print(“Done!”)

In the alert pop up you should see:

Done!

The next node “#chromedriver_3” we will see how to get more information such as errors by doing some “Error Handling” with the try/except blocks.

This will enable us to debug our script and catch any bugs in our scripts. Remember to always write your scripts in a python IDE NOT IN UBOT!!

Lastly but not least, try/except blocks are mostly used while doing I/O operations. That means any reading/writing to files and doing anything over the wire/internet. So, if you don’t see them in example code it is just to have more focus on the example code.

Hopefully that worked for you and we can move on, else we can chat in the comments below.

Third Tab – “chrome options”

sleenium, chrome options, python, ubot

In this tab we will learn how to use chrome options to enable us to use User-Agents, Proxies and some of Chromes command line options. Ok, you can see at the top we have the basic imports required by selenium on line one then line two gives us the delay we need form the time module. Then the variables on lines 4, 6 and 8. I do this because it is easier to read and to stick UBot variables inside the quotes. You will see this in an later tutorials.

Lines 11-17 are the chrome options that add the arguments to control the command line features of the Chrome Browser.

  • Proxy
  • User Agent
  • User Data Directory
  • Disable info bars
  • Disable notifications

Those are just the few of the many command line switches for Chrome.

Next on line 22 and 24 we just want to get the HTML from the page into UBot with the

html = chrome.page_souce variable.

print(html)

On line 29 you see the optional “finally:” block which it is a part of the try/except blocks. There I threw the .close() and .quit().

 

 

 

Posted in Python Selenium.