I'm trying to use selenium on OSX to open a chrome window with existing cookies so that I can bypass login. When I don't add the argument 'chrome_options=option' to open chrome with my user settings, the driver.get function works fine and it opens a chrome window with no extensions loaded and browses to the url. But when I use the code as shown below, it simply opens chrome (with all extensions loaded) but does not browse to the URL. Am I missing something simple?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/Users/me/Library/Application Support/Google/Chrome/")
driver = webdriver.Chrome("./assets/chromedriver",chrome_options=options)
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.gmail.com/")
Related
I would like to use an existing installation of chrome (or firefox or brave browser) with selenium. Like that I could set prespecified settings / extensions (e.g. start nord-vpn when opening a new instance) that are active when the browser is opened with selenium.
I know there is selenium.webdriver.service with the "executeable-path" option, but it doesn't seem to work when you specify a specific chrome.exe, the usage seems to be for the chrome-driver only and then it still opens a "fresh" installation of chrome.
Starting selenium with extension-file I think is also not an option to use with the nord-vpn extension, as I have two-factor authentication active and login every single time would take too much time and effort, if possible at all.
Firefox profile
To use the existing installation of firefox you have to pass the profile path through set_preference() method using an instance of Option from selenium.webdriver.common.options as follows:
from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
profile_path = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
options=Options()
options.set_preference('profile', profile_path)
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
You can find a relevant detailed discussion in Error update preferences in Firefox profile: 'Options' object has no attribute 'update_preferences'
Chrome profile
Where as to use an existing installation of google-chrome you have to pass the user profile path through add_argument() using the user-data-dir key through an instance of Option from selenium.webdriver.common.options as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.google.com/")
You can find a relevant detailed discussion in How to open a Chrome Profile through Python
I am grinding through day 2 of me learning Python 3.7 with Selenium.
I am accessing a web page using WebDriver. I have been making progress, but am stymied now. Though I can easily disable the Firefox password manager popup window on my normal Browser (Options/Privacy and Security/Location/Settings), my script's remotely-run (think that is by definition) browser does not recognize that configuration, and the Firefox popup shows up.
The script can ignore the popup and navigate the target site until the very last page that I need to access. At that point, the HTML for that page is inaccessible, until I manually click on the Firefox popup, dismissing it. As soon as I do that, the HTML code for that web page lights up in Firefox Web Developer Inspector.
Now, that HTML code may be inaccessible for other reasons (like I said, day 2 of the learning curve), but is there some library or commands within Webdriver that allow me to automate the dismissal of that FireFox popup. It is not part of the HTML of any page, so I am at a loss.
Edit: I should mention also, the bulk of that last page's content is blank until I manually dismiss the FireFox popup.
I have added the following code, but still am getting the same popup:
from selenium import webdriver
#Using Firefox to access the Web
options = webdriver.FirefoxOptions()
options.set_preference("dom.webnotifications.enabled", False)
driver = webdriver.Firefox(options=options)
driver.maximize_window()
Second Edit: This is the current code section defining the profile, and I am still getting the pop up password manager.
import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
#Using Firefox to access the Web
profile = webdriver.FirefoxProfile()
#profile.set_preference("dom.push.enabled", False)
profile.set_preference("dom.webnotifications.enabled", False)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.maximize_window()
about:config
preference security.insecure_field_warning.contextual.enabled to false
Trying to open "Google" or any other page (website) from Chrome via selenium chrome driver in python.
The code is :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import time
driver = webdriver.Chrome()
driver.get('https://google.com')
However, this opens my chrome window with the specified link and "data;" tab.
Why that data; tab opens? How to fix it?
Using latest versions of Chrome and Chromedriver
You don't need as much module for this just remove all of those apart from:
from selenium import webdriver
And try again you will not get another tab with congaing data.
import time
time.sleep(1)
driver.switch_to.window(driver.window_handles[1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
time.sleep(1)
I'm not sure if its the same problem, but some time ago I made an exe script to run in another PCs and in one of the PCs selenium just didn't worked with Chrome.
This is the question I posted, but the answers didn't help me, hope it works with you: Chromedriver do not open a new session, it opens a new tab in a existing session
If it doesn't work, I made a workaround to run with Firefox instead of Chrome to ensure it would work properly.
With selenium 4 (or newer), you can use the following code to launch a new browser, (without the Chrome is being controlled message), and then the browser navigates to Google in the same tab:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService()
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://google.com')
I have tried selenium webdriver in Python and it works fine. But when I try to open default chrome profile it doesn't open the websites.
The code is
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("user-data-
dir=/Users/prajwal/Library/Application Support/Google/Chrome")
capability = DesiredCapabilities.CHROME
capability["pageLoadStrategy"] = "normal"
driver = webdriver.Chrome(desired_capabilities=capability,
chrome_options=chromeOptions)
driver.get("https://www.google.com")
The window opens in this case. But it doesnt open the website. However, it works fine if I remove
chromeOptions.add_argument("user-data-
dir=/Users/prajwal/Library/Application Support/Google/Chrome")
Where am I going wrong ?
When I manually open a browser, both firefox and chrome, and proceed to a website where I previously saved my login credentials via the browser, the username and password fields automatically populate as they should. However, when I use the python selenium webdriver to open the browser to a specific page, the fields do not populate.
The point of my script is to open the webpage and use element.submit() to login since the login credentials should already be populated., but the are NOT.
How can i get them to populate in the fields?
For Example:
driver = webdriver.Chrome()
driver.get("https://facebook.com")
element = driver.find_element_by_id("u_0_v")
element.submit()
This is because selenium doesn't use your default browser instance, it opens a different instance with a temporary (empty) profile.
If you would like it to load a default profile you need to instruct it to do so.
Here's a chrome example:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
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)
And here's a firefox example:
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxProfile
profile = FirefoxProfile("C:\\Path\\to\\profile")
driver = webdriver.Firefox(profile)
Here we go, just dug up a link to this in the (unofficial) documentation. Firefox Profile and the Chrome driver info is right underneath it.
download chromedriver.exe: https://chromedriver.chromium.org/downloads
show chrome version and profiles: open chrome and type: "chrome://version/" on URL
from selenium import webdriver
from os.path import abspath
from os import path
from time import sleep
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\...") # Path to your chrome profile or you can open chrome and type: "chrome://version/" on URL
chrome_driver_exe_path = abspath("./chromedriver_win32/chromedriver.exe") # download from https://chromedriver.chromium.org/downloads
assert path.exists(chrome_driver_exe_path), 'chromedriver.exe not found!'
web = webdriver.Chrome(executable_path=chrome_driver_exe_path, options=options)
web.get("https://www.google.com")
web.set_window_position(0, 0)
web.set_window_size(700, 700)
sleep(2)
web.close()
If it navigates to "data/default" you can put your new url in that tab by doing driver.switch_to_window(driver.window_handles[0]).
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Profile num\\")
path = os.path.abspath(filedialog.askopenfile().name)
driver1 = webdriver.Chrome(executable_path=path,options=options)
before you do this open chrome and add a profile then go into the profile and paste the contents of the profile into the default dir