How to get Selenium/GeckoDriverManager driver path in Python - python

Following this issue I encountered yesterday : selenium.common.exceptions.SessionNotCreatedException in Python Selenium using GeckoDriverManager, I would like to know if there was a way to delete at the end of a program execution the driver directory created by GeckoDriverManager.
Here's what I use to put in my codes :
import os, shutil
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
service = FirefoxService(executable_path=GeckoDriverManager().install())
driver = webdriver.Firefox(service=service)
Every time this is executed, GeckoDriverManager creates a folder in my /tmp directory called rust_mozprofile<random chars and digits>. Is there any way or function to get the path to this folder, in order to delete it and its contents, for example using the code below?
def __cleanup(driverDirectoryPath):
if os.path.exists(driverDirectoryPath):
shutil.rmtree(driverDirectoryPath)

Related

Python 3 Selenium Run Multiple Browsers Simultaneously

I'm currently trying to run multiple browsers at the same time with Selenium.
All processes start but don't execute passed functions correctly.
My Code:
from multiprocessing import Pool
# function that creates driver
driver = self.create_driver()
pool.apply_async(launcher.launch_browser, driver)
The launch_browser function is a different Python script that is supposed to run but only a Browser window is opened. (yes, I imported the external script as a module)
You can simply open new browser windows the same way you open the first one, with the same or different browsers:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Bot:
def __init__(self):
self.firstBrowser = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
self.secondBrowser = webdriver.Firefox()
def gotopage(self):
self.firstBrowser.get("https://www.google.com/")
self.secondBrowser.get("https://stackoverflow.com/")
bot = Bot()
bot.gotopage()

How to open a already logged-in chrome browser with undetected_chromedriver python

I want to open an instance of undetected_chromedriver with a pre-set Chrome profile (basically the same thing as this thread asks about but with undetected_chromedriver instead of selenium).
This code works for me, using selenium (the first bit is just cloning the Chrome profile directory into the project directory to avoid an error, based on this thread):
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import undetected_chromedriver as uc
import os
import shutil
# copy Chrome 'Profile 2' folder into this python project folder (for some reason, this is the
# only way Selenium is able to open a chrome browser with a pre-set profile without Chrome getting
# upset about multiple uses of the data directory)
# first, delete the extra files that Chrome automatically adds to user data directories (needed
# if script has already been run)
dir = '{project directory}'
for files in os.listdir(dir):
path = os.path.join(dir, files)
if path not in [{files I want to keep in the project directory}]:
try:
shutil.rmtree(path)
except OSError:
os.remove(path)
# then copy chrome profile into project directory
src = 'C:/Users/{CHROME_USER_DIR}/AppData/Local/Google/Chrome/User Data/Profile 2'
dst = '{project directory}/User Data/Profile 2'
shutil.copytree(src, dst, symlinks=False, ignore=None, ignore_dangling_symlinks=False, dirs_exist_ok=False)
userdatadir = '{project directory}/User Data'
# initialize Chrome instance using Profile 2
options = webdriver.ChromeOptions()
options.add_argument(f"--user-data-dir={userdatadir}")
options.add_argument("profile-directory=Profile 2")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get('https://www.nyt.com/')
BUT, when I try to do the same thing using undetected_chromedriver:
uc_options = uc.ChromeOptions()
uc_options.add_argument(f"--user-data-dir={userdatadir}")
uc_options.add_argument("profile-directory=Profile 2")
driver = uc.Chrome(service=Service(ChromeDriverManager().install()), options=uc_options, use_subprocess=True)
Chrome errors out as so (see screenshot below), and the Chrome instance is not logged into the specified profile!
Thank you in advance for any help!

how to fix executable path in chrome driver error

from selenium import webdriver
from selelium.webdriver.common.keys import keys
from time import sleep
driver = webdriver.chrome(executable_path="C:\Users\dontr\Downloads\chromedriver_win32 (1)\chromedriver.exe")
whenever I run the script I get a syntax error, i have everything downloaded. selenium, chromedriver, I even added it to the path correctly. I think the executable path is broken or something.
There are spelling mistakes in your code, selenium spell wrong 2nd line and Keys instead of keys:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Chrome(executable_path="C:\Users\dontr\Downloads\chromedriver_win32 (1)\chromedriver.exe")

Create a new folder and make it as default download in selenium

I am doing a selenium test in python where I want to create a new directory with current time and make it as default download folder. So whenever I run the script the default download location should be a new directory created at that time and the file should download there.
from selenium import webdriver
from datetime import datetime
import os
today = datetime.now()
current_dir = os.mkdir("/Users/Desktop/" + today.strftime('%Y-%m-%d_%H-%M-%S'))
browser = webdriver.Chrome('/Users/Desktop/chromedriver')
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : current_dir }
chromeOptions.add_experimental_option("prefs",prefs)
I am running the above script and it creates a new folder but the default download location doesn't change as the file still downloads at chrome://Downloads. Is there any way we can change the new created folder as default download directory
You need to create folder name and folder itself in two separate code lines:
current_dir_name = "/Users/Desktop/" + today.strftime('%Y-%m-%d_%H-%M-%S')
os.mkdir(current_dir_name)
and then path current_dir_name into
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : current_dir_name}
chromeOptions.add_experimental_option("prefs", prefs)
The problem here is with the os.mkdir method. The os.mkdir() method in Python is used to create a directory named path with the specified numeric mode. This method does not return any value.
Execution of the code with debugging:
If you look at the above image, you can see that the current_dir is of None type.
So, the folder is created on the desktop, but the path to it is not being captured.
To create a new directory with current time you can use the datetime module as follows:
Code Block:
from datetime import datetime
import os
new_dir = "C:/Users/user-name/Desktop/" + datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
print(new_dir)
if not os.path.exists(new_dir):
os.makedirs(new_dir)
Console Output:
C:\Users\user-name\Desktop\Debanjan\PyPrograms>new_directory.py
C:/Users/user-name/Desktop/2020-08-04_18-01-46
C:\Users\user-name\Desktop\Debanjan\PyPrograms>new_directory.py
C:/Users/user-name/Desktop/2020-08-04_18-02-01
C:\Users\user-name\Desktop\Debanjan\PyPrograms>new_directory.py
C:/Users/user-name/Desktop/2020-08-04_18-02-05
Snapshot of the newly created directories:
This usecase
Now you can implement the same logic to create a new directory on each execution and set as the default download location using Selenium as follows:
from selenium import webdriver
from datetime import datetime
import os
new_dir = "C:/Users/user-name/Desktop/" + datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
print(new_dir)
if not os.path.exists(new_dir):
os.makedirs(new_dir)
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {"download.default_directory" : new_dir})
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
Reference
You can find a couple of relevant detailed discussions in:
Python: Unable to download with selenium in webpage

Python, error with web driver (Selenium)

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('http://arithmetic.zetamac.com/game?key=a7220a92')
element = driver.find_element_by_link_text('problem')
print(element)
I am getting the error:
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
I am not sure whythis is happening, because I imported selenium already.
Either you provide the ChromeDriver path in webdriver.Chrome or provide the path variable
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driverLocation = 'D:\Drivers\chromedriver.exe' #if windows
driver = webdriver.Chrome(driverLocation)
driver.get('http://arithmetic.zetamac.com/game?key=a7220a92')
element = driver.find_element_by_link_text('problem')
print(element)
Best way to eliminate this Exception without altering the code eevn single line is to add the chromedriver.exe( or nay other browser driver files) in to Python
site_packages/scripts directory for windows
dist_package/scripts for Linux
Please check this solution, it works.
If you are using a Mac, then don't include '.exe' I put the selenium package directly into my Pycharm project that I called 'SpeechRecognition'. Then in the selenium file, navigate to: /selenium/webdriver/chrome, then copy and paste the 'chromedriver.exe' file you downloaded most likely from [here][1]
Try this script if you are using PyCharm IDE or similar. This should open a new Google window for you.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome('/Users/Name/PycharmProjects/SpeechRecognition/selenium/webdriver/chrome/chromedriver')
browser.get('http://www.google.com')
Then if you want to automatically search an item on Google, add these lines below and run. You should see an automatic google search window opening up. It might disappear quickly but to stop that, you can simply add a while loop if you want or a timer
search = browser.find_element_by_name('q')
search.send_keys('How do I search an item on Google?')
search.send_keys(Keys.RETURN)
[1]: https://sites.google.com/a/chromium.org/chromedriver/home

Categories