Threading with Python Selenium and the Chrome Browser inside UBot Studio
All this without having to use Chrome Profiles 😉
Let’s get right to it!
Before you try to run anything!!
In the 5th tab there is a UBot function
You must put the path to your Python exe for this code to work.
Tab 1 – from Python list
In this tab I show bare bones almost pure Python script to help keep it as simple as possible to understand. Then in the following tabs we solve some known syntax issues running Python in UBot Studio.
In the first set command #python file location I am importing the “os” module so we can change the directory to where the .ubot/.exe file is, this lets Python know where the Python code is running from.
os.chdir() function changes the directory. os.curdir() shows the current directory.
In the second set command #CHROME BROWSER is where you will get the output from running the code. This code simply returns the page titles of the web pages in the “urls” Python list.
Also, in the python function “chrome_get_titles” we need to tell selenium where the “chromedriver.exe” is to run the Chrome Browser. Notice I am using a relative path and his is where you need to put the chromedriver.exe file after you download it from here
With the above components installed correctly you should be able to run this tab like in the video on the sales page. Same with the following tabs.
Tab 2 – List from file
In the first set command #in_file_path we set the absolute path to the sites.txt file which contain a list of urls selenium can navigate to to get their page names from the HTML titles tag. This absolute path is held in the python variable “filepath”. Before the “” for the string is an “r” which tells python to read it as none code. The file path contains a backslash(\) which is python syntax for new a line.
In the second set command #python code you will notice another Python function “list from fie” which does the same thing as the UBot function except it reads the file into a Python list.
Below the Python functions is the UBot variable #in_file_path which contains the Python code for the sites.txt file. Below that is the function that reads the list from file into the “urls” Python list for us to loop over to get the page titles.
Below that is the Python threading code. Here is a comprehensive tutorial on this piece of code. It is really easy to understand if not watch it again.
Tav 3 – from file -py- append 2 file
In the second set command #python functions I add another Python function “append_file”. What this does is creates a file if it does not exist and appends the url and page title as selenium navigates to each url in the sites.txt file.
The name of the file is set in the UBot variable #out_file_path.
Also notice we are separating our Python code up to keep it organized and easier to locate in a larger bot. Not only that it will help with any hybrid code that needs to be separated so that UBot can render its variable and/or functions.
Tab 4 – Using relative paths
This tab is to better illustrate relative paths.
Tab 5 – Using Python code as a UBot function
In the previous tabs I show how to separate your code into UBot variables. Putting all this into a UBot function is pretty much the same thing except it is better as it is all in one node.
Again the purpose of all this is to help you think about what to do when you have curly braces in your Python code. “F strings” are new to Python 3.6 and I love them, the syntax is much easier than the old way. You can see I used it here in this tab.
In the UBot function $selenium threading inside the “functions” field. Then in the Python function “chrome_get_titles” on this line