Advanced Automation Tips with Python | Selenium
What's up Devs? In this post, I want to share my automation experience from my current internship.
Actually, I wanted to explain it to you on a real project, but unfortunately, I didn't find any platform that fits all my requirements.
However, if you are Python developer you will need these tips to build your automation program fast. So, let's start!
It is important to configure web driver correctly to be able to run automation. If you want to use Chrome as a web driver then you should install chromedriver. However, if you want to choose Firefox then you should install geckodriver.
Creating a class
With creating a class you can easily handle URLs and call functions so you don't have to create web drivers in each function. Take a look following code:
So, when you need a driver in function just write:
For handle login you should add fields into __init__ function. Take a look following code:
Very simple approach to handle logins.
It is important to use time.sleep() if you are looping clicks. Because automation needs at least 2 seconds to perform another click. If you don't use time.sleep() then ElementClickInterceptedException error will appear.
Write data into CSV
There is a lot of solutions and examples on the internet about how to write data to CSV. Let's assume that you have multiple arrays and you want to write them into CSV under the right field names (column name).
Let me be more clear..
My task was to crawl some phrases and translate them to all languages then write all these data into CSV. So, I used GT API and created an array for each language.
I had a lot of arrays so I need somehow write each translation under right field names (column name).
The following code demonstrating the best solution to handle these kinds of problems:
As you see, you should zip all arrays, loop it and write rows dictionary of zipped field names and loop items.
By this way you will write all data correctly into CSV file.
Handle Popups or Iframes
Another challenging task is to handle popups or iframes while automation. If you need to interact with popup or iframe elements, you should tell selenium to switch the window from main to popup. Once you finished with popup you should switch back to the main window.
Great! I am providing you the solutions which are really complicated on the internet.
If you have to handle alert popups you also can use this method.
Send CSV data to web elements
Sometimes you need to send csv data into web elements such as multiple textboxes.
Assume that you have a multiple panels with multiple textboxes in it and you have to send CSV rows into these texboxes
So each CSV row belongs different panels. Sounds crazy right? This is how you will train your brain :) Somehow you have to iterate each CSV row for each panel's textboxes.
Before we go on please visit Reverse Python if you want to find more articles like this.
Alrgiht! Let's see the code and then I am going to explain:
Index plays the main role in this solution. We said that we want to handle multiple panels, so the index prevents from the loop all rows in each panel. Assume that you have 3 rows and without using the index, the program will continue to loop until the 3rd row finished. As a result, you will have 3 values for each textbox. However, we need to send only first row values into first panel's textboxes, second row values into second panel's textboxes and so on..
Once index is defined, we are iterating textboxes. By this way, first value in row is going to send first textbox, second value in row is going to send second textbox and so on..
These examples are from the real-world project so I recommend bookmarking this post.
So there are 2 approaches for handling dropdwons.
First solution is to use selenium's Select method to select options from dropdown.
and second solution is just using xpath to click dropdown items:
Navigate parent element with xpath
Sometimes Html structures can be messed up and you can't select the element you want. In these cases, it is good to navigate the parent element or go back using xpath:
That is all for now! I shared my python automation experience with you and I hope it helps you and saves your time from searching on the internet.
See you very soon DEVs! Stay Connected!