Python Scripts to set browser default homepage in IE and clear cache for Chrome, Firefox and Internet Explorer?
In the Automated world, we may be wondering if we can change the settings of browsers using Python scripts for your task, We will discuss how we can use python scripts to perform different operations on installed browsers in Windows PC.
We will use Python scripts for below points.
- Python Scripts to set default browsers homepage in Internet Explorer
- Python Scripts to Clear cache for Chrome, firefox and internet explorer
Python Scripts to set default browsers homepage in Internet Explorer:
Sometimes we see while opening the Internet Explorer a default URL used to comes up in the homepage which may advertisements or something unwanted pages which may cause a delay in loading the browser which may affect for some automation if you are running on IE or some similar delay sensitive tasks.
As a solution, you may be seeking a solution to set the default IE homepage (any URL you want) using Python scripts.
Here is the code for setting the default IE homepage:
import subprocess
import os
powershell_dir = r'C:\Windows\System32\WindowsPowerShell\v1.0'
powershell_exe = 'powershell.exe'
powershell = os.path.join(powershell_dir, powershell_exe)
print(powershell)
powershell_cmd = "$path = 'HKCU:\Software\Microsoft\Internet Explorer\Main\';$name = 'start page';$value = 'http://google.com/';Set-Itemproperty -Path $path -Name $name -Value $value"
subprocess.call([powershell, powershell_cmd])
Explanation: As we know Powershell is the best way to interact with Microsoft OS applications, so we wrote scripts using Python to call the Powershell commands to execute the operations. In the code we set our IE home page to google.com by default.
Note: This PowerShell command may not work in Organisation systems as we don't have permission to change the registry of the System due to security purpose,
IE by default being written to the system registry where your organizations do not give permission to modify.
Python Scripts to Clear cache for Chrome, firefox and internet explorer:
Here below is the scripts to clear different browser cache using Python Scripts.
import subprocess, os, time
bld=r"""
rem IE
start "" "C:\Windows\System32\rundll32.exe" InetCpl.cpl,ClearMyTracksByProcess 255
@rem Clear IE cache - (Deletes Temporary Internet Files Only)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
erase "%LOCALAPPDATA%\Microsoft\Windows\Tempor~1\*.*" /f /s /q
@rem Clear Google Chrome cache
erase "%LOCALAPPDATA%\Google\Chrome\User Data\*.*" /f /s /q
@rem Clear Firefox cache
erase "%LOCALAPPDATA%\Mozilla\Firefox\Profiles\*.*" /f /s /q
"""
#Creating a batch file to perform the operation
path=r"C:\Users\Desktop\batchFile_CacheClear.bat"
with open(path,'w') as f:
f.write(bld)
# Running the batch file through command prompt to clear the cache.
os.system(r"C:\Windows\System32\cmd.exe /c"+path)
#You can remove/delete the batch file after operation using below commands
#os.remove(path)
Here we wrote batch file to excute the operation to clear the browser chaches and cookies and we are running that batch file
using Python scripts.
I hope it will be helpful for your tasks.

Nice work Rajesh !!! I was trying to set home page in IE instead of the default mssn.xx.yy. This works perfectly fine as intended, Thanks for this.
ReplyDelete