i am using python ana selenium, to automate some process, but couldnt attached selenium to default chrome profile
i tried with,
capability = webdriver.DesiredCapabilities.CHROME
self.driver = webdriver.Remote('http://127.0.0.1:9515/wd/hib',capability)
of course, i started, chromedriver first, and also tried with,
import time
from selenium import webdriver
import selenium.webdriver.chrome.service as service
service = service.Service('./chromedriver')
service.start()
capabilities = {'chrome.binary': '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'}
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
driver.quit()
this causes, selenium.common.exceptions.WebDriverException: Message: u'Could not find Chrome binary at:
and also tried with,
self.driver = webdriver.Chrome("./chromedriver")
this works, but not default profile, and also wonder to know, how to open new window or new tab with this ?
thanks.
Don't just copy/paste something straight off the website! Have a look into that folder yourself, does it have anything in it?! My guess is no. This is why when you leave that bit off, it works fine, because it's looking for Chrome where it should exist!
Any way, more to the point you are using it wrongly!
If you want to give Selenium a different profile to use for Chrome, then you need to use the options class:
https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/chrome/options.py
You want the add_argument function.
Why?
This is because to give Chrome another profile to use, you need to launch Chrome with a specific command line (specifically --user-data-dir):
http://www.chromium.org/user-experience/user-data-directory
The add_argument function exposes the ability to add command line switches.
So if you use the add_argument function, Selenium will simply pass whatever you give it, down to Chrome as being part of it's command line switches.
To find out where your chrome profile is located start chrome and type
chrome://version
in the address bar. Under "Profile Path:" you'll see the location of the profile you're currently using. For example:
~:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
Related
I am trying to build a TinderBot.
Both Chrome and FireFox ask for geo permission outside of code (the pop-up drops from the browser's address bar, so it's not inside the html and I cannot access it with .find_element)
I found some prompts on Chrome here: https://testingbot.com/support/selenium/permission-popups (didn't try it though, so not sure if they are up to date)
But I cannot find anything for FireFox.
I found this piece of code that disables javascipt
https://www.selenium.dev/documentation/webdriver/capabilities/firefox/
And I believe that I could build on it, but I cannot find how to set it so that it gives permission for geolocation.
Recently I though I found here a piece that at least might show me how to correctly pass '-hedless' argument but it won't open browser now.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.add_argument('-headless')
srvc = Service(GeckoDriverManager().install())
driver = webdriver.Firefox(options=opts, service=srvc)
So generally I have 2 problems here:
How do I make add_argument work? (I mean other cases turned out deprecated)
What arguments do I need to target to allow geolocation on launching browser with the bot?
Am I even on the right path? I cannot ask questions in relevant threads because of insufficient rating, so here I am.
To initiate Firefox browser in headless mode instead of using add_argument() you need to set the headless property to true as follows:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.headless = True
srvc = Service(GeckoDriverManager().install())
driver = webdriver.Firefox(options=opts, service=srvc)
You can find a relevant discussion in How to make Firefox headless programmatically in Selenium with Python?
I'm trying to use IE using the selenium module in python.
I want to log in with different login IDs in multiple IE windows, but the login information in IE windows is shared with each other, so independent login is not possible.
I am using selenium version is 3.6 and the explorer version is 11.
How can I fix it?
For example, when I log in to Google email in the first explorer window, when the second explorer window is turned on, I am already logged in to the Google email.
I want to log in to the same site with a different ID at the same time.
IE_1 = ID_1
IE_2 = ID_2
IE_3 = ID_3
....
You can achieve separate logins by running the browser in "private" mode.
This is not specific to IE. You can do this in Chrome Firefox, and other browsers as well.
1. IE in private mode
From the docs, you can set "IE Command-Line Options".
Example below is directly from the documentation.
I have NOT tested the below code myself
as I don't have IE in my environment.
from selenium import webdriver
options = webdriver.IeOptions()
options.add_argument('-private')
options.force_create_process_api = True
driver = webdriver.Ie(options=options) # MIGHT WANT TO CHANGE
# Navigate to url
driver.get("http://www.google.com")
driver.quit()
If you don't have the $PATH set for IE, try this:
driver = webdriver.ie(executable_path="<YOUR_IE_PATH>",
options=options)
2. Chrome in private mode
For Chrome, I can confirm it is valid working code. It's a simplified version of what I use myself.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(
executable_path="<YOUR_CHROME_DRIVER_PATH>",
chrome_options=chrome_options)
driver.get('https://google.com')
For the full list of acceptable "chrome_options", you can check out the reference. This link may not look "official", but it's actually referenced from the Chromedriver website (under "Recognized Capabilities" - "ChromeOptions object" - "args"), so we can safely rely on this doc.
I want to use selenium with my Google profile, I created this code:
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=C:\Users\myprofile\AppData\Local\Google\Chrome\User Data") #Path to my chrome profile
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe", options=options)
driver.get(url)
I got this error:
InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
I also tried another profile, but return the same error:
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/mxs/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe", options=options)
driver.get(url)
also, I tried codes in both Jupyter Notebook and VSCode
Suggestion 1
If you are okay with only one selenium process running at a time, you can use queue to achieve it. As long as you use only one process to access user-dir, it'll not cause any error.
Suggestion 2
If you are trying to stay logged in for some website, you can reuse cookies. In this case you don't need to use user-data-dir thing. Selenium will automatically create temporary user-data-dir on every run.
Suggestion 3
If you still want to use the same profile for every session, here is the hack to do it. You keep original --user-data-dir unused, instead you copy all the content of your original profile directory to some temporary directory and use it with --user-data-dir every time you initiate chrome selenium.
Suggestion
When you are trying to run your python script close all the chrome windows. I got the error
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
when I had one of the chrome windows open. So it is better to close all the chrome windows and try again.
I want to be able to use pure selenium webdriver to open a zoom link in Chrome and then redirect me to the zoom.us application.
When I execute this:
from selenium import webdriver
def main():
driver = webdriver.Chrome()
driver.get("https://zoom.us/j/000-000-000")
main()
I receive a pop-up saying
https://zoom.us wants to open this application.
and I must press a button titled open zoom.us to open the app.
Is there a way to press this pop-up button through selenium. Or, is there some other way to open zoom from chromedriver?
NOTE: I only want to use selenium. I have been able to implement pyautogui to click on the button but that is not what I am looking for.
Solution for Java:
driver.switchTo().alert().accept();
Solution for Python:
driver.switch_to.alert.accept()
There are a lot of duplicated questions regarding this issue. Here is one of them, and it is quite sure that selenium is not capable of achieving such job since it only interacts with the chrome page. I previously encountered this issue as well and here is my solution to it. It might look really unprofessional, but fortunately it works.
The logic of my solution is to change the setting of chrome in order to skip the popup and directly open the application you want. However, the Chrome team has removed this feature in the latter version for some reasons, and we need to get it back manually. Then, we know that everytime when selenium starts to do the thing it opens a new Chrome page with NO customized settings just like the incognito page. Therefore we need to do something to let selenium opened a Chrome page with your customized setting, so that we can make sure that the popup, which we changed manually to skip, can be skipped successfully.
Type the following code in your terminal.
defaults write com.google.Chrome ExternalProtocolDialogShowAlwaysOpenCheckbox -bool true
This enables you to change the setting of skipping popups, which is the feature Chrome team removed.
Restart Chrome,and open the zoom (or whatever application) page to let the popup display. If you do the 1st step correctly you will be able to see there is a checkbox shown next to the "Open Zoom.us" saying if you check it chrome will open this application without asking, that is, to skip the popup for this application.
Now we need to let selenium open the Chrome with our customized setting. To do this, type "chrome://version" in the search tab of your ordinary Chrome (Not automated page opened by selenium). Go to "Profile Path", and copy this path without the last word "default". For example:
/Users/MYNAME/Library/Application Support/Google/Chrome/Default
This is my profile path, but I only copy everything except the last word Default, so this is what I need to copy.
/Users/MYNAME/Library/Application Support/Google/Chrome/
This is for Mac users, but for Windows only the path is different(starts with C:// or something), steps are same.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
option = Options()
option.add_argument('THE PATH YOU JUST COPIED')
driver = webdriver.Chrome(executable_path='YOUR PATH TO CHROMEDRIVER', options=option)
driver.get("google.com") #Or anything else
We use "options" to let selenium open a page with our customized profile. Now you will see selenium opens a Chrome page with all your account profile, settings, and it just appears like your ordinary chrome page.
Run your code. But before that, remember to quit ALL CHROME sessions manually. For Mac, make sure that there is no dot under Chrome icon indicating that Chrome is not running for any circumstances. THIS STEP IS CRITICAL otherwise selenium will open a chrome page and it just stops there.
Here are all the steps. Again, this solution is vert informal and I personally don't think it is a "solution" to this problem. I will try to figure out a better way of achieving this in the future. But I still posted this as an alternative simply because I guess it might be helpful to some extent for somebody just like me. Hope it works for you, and good luck.
I develop a program, which checks browser plugin behaviour [ in python & selenium ]. The plugin is a black box to me I just have it installed in browsers.
For example, in Firefox, i've installed it to my profile and force webdriver to open firefox with desired profile. I've made the same witch chrome, and it obviously loads the profile, but not the extension.
opt = webdriver.ChromeOptions()
opt.add_argument("--user-data-dir=C:\\Users\\..\\Google\\Chrome\\User Data\\Profile 2\\")
driver = webdriver.Chrome("C:\\...\\chromedriver.exe", chrome_options=opt)
Is there any way how to make this functionallity work? Or is the problem, that the extension are bound to the logged user, not profile of browser?
What I need to do is to execute chrome with specified url, let the plugin to analyze the site and then check plugin's output.
Another problem is that I am not the one who will use the program, so I wouldn't like to start force user to pack crx extenstion (or is there a simple way to this?). My requirments is that thare is one or two items in configuration file, like
chrome-profile: path/to/profile
Thank you for any advice.