How to create Python Selenium Chrome webdriver remote with Current user data? - python

How I create LOCAL Chrome Webdriver WITH Current user data
chromedriver = "/Users....../chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=/Users..../Google/Chrome")
webdriver.Chrome(executable_path=chromedriver,
chrome_options=options)
How I create REMOTE Chrome webdriver with clear - new user data
webdriver.Remote(command_executor="http://192.168.1.30:4441/wd/hub",
desired_capabilities=DesiredCapabilities.CHROME)
Now, How create Remote Chrome webdriver with current user data?

Try this:
os.environ["webdriver.chrome.driver"] = chromedriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/Users..../Google/Chrome")
webdriver.Remote("http://192.168.1.30:4441/wd/hub",
options.to_capabilities())
And if you have your chromedriver.exe on the PATH then you should not need this part:
chromedriver = "/Users....../chromedriver"
Not sure if that works for you, but here is an example that let me start remote chrome webdriver with desired chromeOption for language:
options = webdriver.ChromeOptions()
options.add_argument("--lang=de")
chrome_remote = webdriver.Remote('http://hostname:4444/wd/hub', options.to_capabilities())

Related

Can't load chrome profile with selenium (Python)

im tryng to load my default chrome profile but instead of opening my profile it open a new istance of chrome
from selenium import webdriver
#object of ChromeOptions class
o = webdriver.ChromeOptions()
#adding Chrome Profile Path
o.add_argument = {'user-data-dir':r'C:\Users\liamm\AppData\Local\Google\Chrome\User Data\Default'}
#set chromedriver.exe path
driver = webdriver.Chrome(executable_path=r"C:\Users\liamm\Desktop\selenium\driver\chromedriver.exe", options=o)
#maximize browser
driver.maximize_window()
#launch URL
driver.get("https://www.tutorialspoint.com/index.htm")

How to update the Proxy Server within the same session using Selenium and Python

How is it possible to change the proxy server in Selenium after starting the driver?
I saw several threads on this topic, but none of the answers were correct.
You can use not only Chrome but also Firefox.
If you have any ideas on how to change the proxy when the driver is open, please write.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
Path = ChromeDriverManager().install()
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-setuid-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=600,400')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-accelerated-2d-canvas')
# options.add_argument('--no-zygote')
# options.add_argument('--single-process')
options.add_argument('--disable-gpu')
options.add_argument('--headless')
proxy_file = open("proxy.txt", "r", encoding="utf-8", errors="ignore").readlines()
proxy = ((random.choice(proxy_file)).replace("\n", ""))
options.add_argument('--proxy-server=%s' % proxy)
browser = webdriver.Chrome(Path, options=options)
browser.get('https://google.com')
# Here the proxy should change
browser.get('https://google.com')
No, you won't be able to change the proxy server using Selenium after starting the driver and the Browsing Context.
When you configure an instance of a ChromeDriver with ChromeOptions() to span a new Chrome Browsing Context the configuration gets baked within the chromedriver executable which will persist for the lifetime of the WebDriver and being uneditable. So you can't modify/add any existing/new configuration through ChromeOptions() class to the WebDriver instance which is currently in execution.
Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies, UserAgent and other session attributes from the already initiated ChromeDriver and Chrome Browsing Session still you won't be able to change the set of attributes of the ChromeDriver.
A cleaner way would be to quit() the existing ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of proxy configuration as follows:
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-setuid-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=600,400')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-accelerated-2d-canvas')
options.add_argument('--disable-gpu')
options.add_argument('--headless')
urls_to_visit = ['https://www.google.com/', 'https://stackoverflow.com/']
proxies = open("proxy.txt", "r", encoding="utf-8", errors="ignore").readlines()
for i in range(0, len(urls_to_visit)):
proxy = ((random.choice(proxies)).replace("\n", ""))
options.add_argument('--proxy-server=%s' % proxy)
browser = webdriver.Chrome(Path, options=options)
browser.get("{}".format(urls_to_visit[i]))
# perform the tasks
driver.quit()
References
You can find a couple of relevant discussions in:
How to rotate Selenium webrowser IP address

Open a chrome profile in python that is already signed into google

I'm trying to open up my chrome profile which is signed into google however am not having any luck.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument(r'user-data-
dir=G:\Users\Kareem\AppData\Local\Google\Chrome\User Data\Profile 10')
chrome_path= r"G:\Users\Kareem\Documents\Work\Computer
Science\Selenium\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.google.co.uk/webhp?source=search_app")
When you create the driver, you're not actually passing the options to it.
driver = webdriver.Chrome(chrome_options=options)

Disable python chrome driver extensions without lose driver path

I started having issues running a python script which uses selenium and chrome driver, so I want to disable extensions of chrome driver using python without losing the path where the driver is located.
Currently I have the below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
path_to_Ie = 'C:\\Python34\\ChromeDriver\\ChromeDriver.exe'
browser = webdriver.Chrome(executable_path = path_to_Ie)
url = 'https://wwww.test.com/'
browser.get(url)
and I would like to add the below lines:
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
browser = webdriver.Chrome(chrome_options=chrome_options)
Any idea how I can merge both codes to make it work properly?
Thanks a lot.
Just provide multiple keyword arguments:
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
browser = webdriver.Chrome(executable_path=path_to_Ie, chrome_options=chrome_options)

set "user-data-dir" in selenium python for chrome driver in Ubuntu [duplicate]

I'd like to launch Chrome with its default profile using Python's webdriver so that cookies and site preferences persist across sessions.
How can I do that?
This is what finally got it working for me.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
To find path to your chrome profile data you need to type chrome://version/ into address bar . For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default, to use it in the script I had to exclude \Default\ so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data.
Also if you want to have separate profile just for selenium: replace the path with any other path and if it doesn't exist on start up chrome will create new profile and directory for it.
This solved my problem. (remove Default at the end)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/username/.config/google-chrome")
cls.driver = webdriver.Chrome(options=options,
executable_path="./../ext/chromedriver")
Chrome_Options ist deprecated. Use options instead
I solved my problem with answer of "Yoannes Geissler".
In my case My profile was named "Profile 2"
My Code :
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/GOD/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')
wd = webdriver.Chrome(options=options)
The below line solved my problem:
options.add_argument('--profile-directory=Profile 2')
Just to share what worked for me. Using default's profile was complicated, chrome keeps crashing.
from pathlib import Path
from selenium import webdriver
driver_path = Path("{}/driver/chromedriver75.exe".format(PATH_TO_FOLDER))
user_data_dir = Path("{}/driver/User Data".format(PATH_TO_FOLDER))
options = webdriver.ChromeOptions()
# TELL WHERE IS THE DATA DIR
options.add_argument("--user-data-dir={}".format(user_data_dir))
# USE THIS IF YOU NEED TO HAVE MULTIPLE PROFILES
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(executable_path=driver_path, options=options)
driver.get("https://google.com/")
By doing this Chrome will create the folder User Data and keep all the data in it where I want and it's easy to just move your project to another machine.
This answer is pretty simple and self-explained.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
exec_path_chrome = "path/to/Google Chrome" #Do not use this path that is extracted from "chrome://version/"
exec_path_driver = "path/to/chromedriver"
ch_options = Options() #Chrome Options
ch_options.add_argument("user-data-dir = /path/to/Chrome Profile") #Extract this path from "chrome://version/"
driver = webdriver.Chrome(executable_path = exec_path_driver, options = ch_options) #Chrome_Options is deprecated. So we use options instead.
driver.get("https://stackoverflow.com/a/57894065/4061346")
As #MadRabbit said type chrome://version/ into the address bar to find the path to your chrome profile data.
It appears like this in Windows C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
It appears like this in Mac /Users/user/Library/Application Support/Google/Chrome/Default
So all you have to do is to erase the last portion Default from the profile path.
Note: Make sure you don't run more than one session at the same time to avoid problems.
This is what I did.
import chromedriver_binary
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument(r"--user-data-dir=User Data Directory")
chrome_options.add_argument(r"--profile-directory=Profile name")
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable
logging"])
chrome_options.add_experimental_option("excludeSwitches", ["enable
automation"])
chrome_options.add_argument("start-maximized")
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.wait = WebDriverWait(self.driver, 20)
Here we do not need to give the chrome driver path.

Categories

Resources