Writing a Log Keeper in Python with Selenium WebDriver

Alex Slepinin
3 min readSep 5, 2020

Let’s keep automating interesting things. Today I will present you another example of collaboration of Selenium WebDriver and Python. We will automate the scenario of keeping local weather log in Excel file.

The script will capture the location associated with user’s IP address, collect local temperature and humidity values from Google and write them to Excel sheet. And if you need help installing Selenium and Python, the guide is here: Installing Selenium WebDriver with Python and PyCharm From Scratch

Let’s code!

The full code is here

We are importing two libraries to work with Excel files: xlswriter (to create a file) and openpyxl (to work with a file).

After specifying Chrome Driver path we are going to ip2locations.com and capturing current location using find_element_by_id method. Next we’re scrolling down the page until city name element is visible and waiting while it’s loading using while loop.

Then we are googling weather for the city identified by IP earlier and saving temperature and humidity values for future use. Now seeing if the temperature is displayed in Fahrenheit or Celsius.

Going to worldtimeserver.com and using explicit wait for date and time elements to appear, then grabbing date, time, and timezone values.

If the excel file doesn’t exist yet, we’re creating it and filling up the first row of our table.

Now we are setting columns to specified width, making the first row bold, and saving the file.

Then we are opening the Excel file, counting how many rows are already filled and writing date, time, temperature, humidity, and time zone values into a new row. Then saving the file and the work is complete!

Every time we run the script it’s going to add a new row to the sheet. Now you can setup a scheduler to run your script as often as needed to keep track of your hometown weather. Or keep track of anything else in the whole internet!

--

--