How to run Explorer independently with Python Selenium - 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.

Related

Automate webpage that just work properly on IE using selenium on Chrome

I have a webpage that just work properly on IE, In other browsers I get a messed up layout, but I need to automate it with headless mode, can selenium work on chrome even with the layout messed?
Does this webpage work fine in Chrome? If this is the case, you can implement this requriement via chrome driver with Selenium.
But if this webpage only work correctly in IE, the you can only implement automation in IE or Edge IE mode with Selenium. And Edge IE mode tabs actually running Internet Explorer, so they all use IE driver.
Here is a simple example:
from selenium import webdriver
from selenium.webdriver.ie.service import Service
ieOptions = webdriver.IeOptions()
ieOptions.add_additional_option("ie.edgechromium", True)
ieOptions.add_additional_option("ie.edgepath",'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe')
ser = Service(r"C:\Users\Administrator\Desktop\IEDriverServer.exe")
driver = webdriver.Ie(service = ser, options=ieOptions)
driver.maximize_window()
driver.get('https://www.google.com/')
Note: Please modify the path parameters according to your own situation.

Where to find all available preferences for selenium for FireFox/Chrome and how to implement them with Python?

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?

How to prevent being signed out of your Google account on a Chrome profile opened in Selenium Python

I managed to start Selenium with a custom Chrome Profile that is signed into a Google Account using ChromeOptions by changing the location of the Chrome Profile folder in my directory, renaming it to "Default" and omitting the "Default" in the path in the argument (cf. explanation here).
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(r"user-data-dir=C:\ChromeProfiles1")
s=Service(r"C:\Users\Administrator\Desktop\chromedriver.exe")
driver = webdriver.Chrome(service=s, options=options)
driver.get(r"https://drive.google.com")
However, as soon as the browser opens, I am signed out of my Google account. It only stays logged in for a split second. Here's a screen capture of it.
Also, I have tried creating new paths to chrome profiles multiple times and only when using it for the first time respectively the icon of the Google account appears for a brief moment. All of the tries after that make it start as "signed out" right away (the way it looks in my recording after the blue icon disappears). Google still seems to realise that the browser is automated; is there a way around this? Can this be fixed by a change to the Chrome profile?

selenium chrome attach default profile

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

Creating a headless Chrome instance in Python

This question describes my conclusion after researching available options for creating a headless Chrome instance in Python and asks for confirmation or resources that describe a 'better way'.
From what I've seen it seems that the quickest way to get started with a headless instance of Chrome in a Python application is to use CEF (http://code.google.com/p/chromiumembedded/) with CEFPython (http://code.google.com/p/cefpython/). CEFPython seems premature though, so using it would likely mean further customization before I'm able to load a headless Chrome instance that loads web pages (and required files), resolves a completed DOM and then lets me run arbitrary JS against it from Python.
Have I missed any other projects that are more mature or would make this easier for me?
Any reason you haven't considered Selenium with the Chrome Driver?
http://code.google.com/p/selenium/wiki/ChromeDriver
http://code.google.com/p/selenium/wiki/PythonBindings
This question is 5 years old now and at the time it was a big challenge to run a headless chrome using python, but the good news is:
Starting from version 59, released in June 2017, Chrome comes with a headless driver, meaning we can use it in a non-graphical server environment and run tests without having pages visually rendered etc which saves a lot of time and memory for testing or scraping. Setting Selenium for that is very easy:
(I assume that you have installed selenium and chrome driver):
from selenium import webdriver
#set a headless browser
options = webdriver.ChromeOptions()
options.add_argument('headless')
browser = webdriver.Chrome(chrome_options=options)
and now your chrome will run headlessly, if you take out options from the last line, it will show you the browser.
While I'm the author of CasperJS, I invite you to check out Ghost.py, a webkit web client written in Python.
While it's heavily inspired by CasperJS, it's not based on PhantomJS — it still uses PyQt bindings and Webkit though.
I use this to get the driver:
def get_browser(storage_dir, headless=False):
"""
Get the browser (a "driver").
Parameters
----------
storage_dir : str
headless : bool
Results
-------
browser : selenium webdriver object
"""
# find the path with 'which chromedriver'
path_to_chromedriver = '/usr/local/bin/chromedriver'
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
chrome_options.add_experimental_option('prefs', {
"plugins.plugins_list": [{"enabled": False,
"name": "Chrome PDF Viewer"}],
"download": {
"prompt_for_download": False,
"default_directory": storage_dir,
"directory_upgrade": False,
"open_pdf_in_system_reader": False
}
})
browser = webdriver.Chrome(path_to_chromedriver,
chrome_options=chrome_options)
return browser
By switching the headless parameter you can either watch it or not.
casperjs is a headless webkit, but it wouldn't give you python bindings that I know of; it seems command-line oriented, but that doesn't mean you couldn't run it from python in such a way that satisfies what you are after. When you run casperjs, you provide a path to the javascript you want to execute; so you would need to emit that from Python.
But all that aside, I bring up casperjs because it seems to satisfy the lightweight, headless requirement very nicely.

Categories