selenium.webdriver.firefox.options - what is it about? - python

I'm looking at this code:
#! python3
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_headless()
assert opts.headless # Operating in headless mode
browser = Firefox(options=opts)
browser.get('https://duckduckgo.com')
source: https://realpython.com/modern-web-automation-with-python-and-selenium/
the idea is to call a headless browser but I don't understand the logic behind this code. What is 'options', and what is 'Options'? What do they exactly do? what options=opts stands for?
Now trying to run this code and the webpage duckduckgo won't open. Any idea why?

Options is a class in the selenium firefox webdriver package.
opts is an instance of the Options class instantiated for the program.
When the code says:
opts = Options()
Python creates an instance of the class, and uses the the variable opts as the access point.
When the code says:
opts.set_headless()
Python is updating the instance of Options, to store the information “the user of this wants to start a headless instance of the browser”
When the code says:
browser = Firefox(options=opts)
Python is creating an instance of the Firefox class, and sending it the opts variable to configure the new instance. In this case, the only option that has been modified from the defaults is the headless flag.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
#--| Setup
options = Options()
options.add_argument("--headless")
caps = webdriver.DesiredCapabilities().FIREFOX
caps["marionette"] = True
browser = webdriver.Firefox(firefox_options=options, capabilities=caps, executable_path=r"geckodriver.exe")
#--| Parse
browser.get('https://duckduckgo.com')
logo = browser.find_elements_by_css_selector('#logo_homepage_link')
print(logo[0].text)
this code works ( gives output About DuckDuckGo ). I was told that opts.set_headless() is deprecated, maybe that's why it didn't give me any result.

Related

How to use a existing chrome installation with selenium webdriver?

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

How does one implement the headless option in the Selenium 4 WebDriver-Manager?

I have but one hurdle to overcome before I can truly call my first bot complete and that is to figure out where to put the options class(?) in order to run ChromeDriverManager in headless mode, and so it stops opening chrome instances! The way the driver is called is:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.headless = True
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
Since the old method of calling webdriver by path hasn't been entirely deprecated yet I don't think there have been very many questions pertaining to the new webdriver-manager. I've found only one or two methods that didn't work, like adding ,options=options after .install() or .options somewhere in the mix. In any case, any suggestions would be appreciated.
try this:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
I typed out this comment and never finished it, so my apologies. The correct code to run selenium 4 WebDriver-Manager in headless mode is indeed:
options = Options()
options.headless = True #
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
#as opposed to what I was trying: #
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install(),options=options))
I imagine that 'options' just needs to be a direct argument of webdriver.Chrome(), so I think this should also work:
driver = webdriver.Chrome(service=Service,options=options(ChromeDriverManager().install()))
Also, I think I have figured out that headless mode makes it easier for websites to mark you as a bot and prompt you to do captchas as after some time of running, because of either captchas or an error in a change of code, my bot couldn't use the search function while headless was true, but performed perfectly with it disabled.

Webdriver Manager+Chrome Headless+Selenium+Python: webdriver does not respond to options

System setup:
I used Python 3.10 in my setup.
I used Selenium 4.
I used the Python webdriver manager in my test setup. (https://pypi.org/project/webdriver-manager/)
I used unittesting for some test pytest.
Details:
Currently, my test setup and the included test cases work under Chrome as a normal GUI call, and the test cases run through under those conditions.
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
class OpenDevcrowd(unittest.TestCase):
def setUp(self):
#self.driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.com/"
self.verificationErrors = []
self.accept_next_alert = True
I have used the options from 2020/2021 to change my setup to headless.
I tried the following information from stackoverflow in my build, but had no success with it at all:
webdriver-manager not using proxy in order to update
Running Selenium with Headless Chrome Webdriver
Issue:
It seems that the options (and I tried almost all the options from the information already found in stackoverflow) would not work anymore?
Only the GUI is still called, and the headless option is simply not used.
Questions:
Did the command range change somehow from 2020/21 to 2022?
Is it the webdriver manager that prevents the options from being called?
--headless should come with --window-size
Ex: "--window-size=1920,1080"

How to launch a Chromium application using Chromedriver?

Is it possible to port this python code to work in Robot Framework?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
chrome_options = Options()
chrome_options.binary_location = 'C:/Program Files (x86)/MyApp.exe'
driver = webdriver.Chrome('C:/Program Files (x86)/chromedriver.exe', chrome_options=chrome_options)
I'm trying to create a chrome webdriver in robot that sends my selenium calls to my chromium application. Is it possible?
I create a python Library see code below but it just launches my app and closes it. I want to be able to make selenium/robot calls to it.
Code for myLibrary.py
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from robot.libraries.BuiltIn import BuiltIn
def pas_webdriver_instance():
se2lib = BuiltIn().get_library_instance('ExtendedSelenium2Library')
chrome_options = Options()
chrome_options.binary_location = 'C:/Program Files (x86)/myapp.exe'
driver = webdriver.Chrome('C:/Program Files (x86)/chromedriver.exe', chrome_options=chrome_options)
You could simply expand Selenium2Library and use your Python code directly. To expand Selenium2Library you can either make a new class that inherits the original library, or get the runnung library's instance on runtime.
Or, if you absolutely want to port this into Robot Framework, you can try out this keyword:
Open Chrome
[Arguments] ${url} ${binary_path}
${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${chrome_options} binary_location ${binary_path}
Create Webdriver Chrome chrome_options=${chrome_options}
Go To ${url}
Edit: To answer your question in comments,
That depends on the version of Selenium2Library you're using since the library got reworked heavily in it's last version, but if you're using versions prior to it's version 3, you can see an example of expansion that gets an instance here (see the property _s2l), and an example of class inheritance here (see the declaration of the class).
I haven't looked into expanding the last version of SeleniumLibrary yet so I couldn't tell you for this particular case. Retrieving the library's instance on runtime should still work fine though.

How to disable logging using Selenium with Python binding

Simple question: how to completely disable logging when using Selenium from Python bindings, ex code as follows:
browser = webdriver.Chrome()
I've tried things like:
options = webdriver.ChromeOptions();
options.add_argument('--log-level 3')
browser = webdriver.Chrome(chrome_options=options)
or even:
options = webdriver.ChromeOptions();
options.add_argument('--disable-logging')
browser = webdriver.Chrome(chrome_options=options)
but still the file 'chromedriver.log' is appearing on each new run of the tests.
You may set options.add_argument("--log-level=3") for Chrome browser to be run with Selenuim, or you may set logging level to some higher level with:
import logging
logger = logging.getLogger('selenium.webdriver.remote.remote_connection')
logger.setLevel(logging.WARNING) # or any variant from ERROR, CRITICAL or NOTSET
But some messages will appear anyway in this case, including the starting DevTools message or SSL handshake error messages.
To run Chrome browser with Selenium in console in completely silent mode, you should use this snippet:
options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])
That trick will suppress any console message from either the Selenium driver or the browser itself, including the first message DevTools listening on ws://127.0.0.1 at the very start.
At the same time some runtime step-by-step data can be saved to service log file, in case its argument has been added.
driver = webdriver.Chrome(service_log_path='/dev/null')
The source code of Chrome's webdriver, shows the existence of an option called service_log_path.
So if you want to get rid of the file, you could set this property to
/dev/null if you are running under Linux/Unix ;
NUL under windows
Hope it helps
Just example for Windows people:
webdriver.Firefox(log_path='NUL')
Accepted answer is correct, but if you are new to Python / windows like i am, example like this will cut you few hours of google time.
To disable logging using Selenium and Python you need to add an experimental option through an instance of ChromeOptions() as follows:
add_experimental_option('excludeSwitches', ['enable-logging'])
Implementation
selenium4 compatible code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
this worked for me:
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
courtesy of:
https://joshuatz.com/posts/2020/selenium-webdriver-disabling-chrome-logging-messages/
if you set service_log_path = None, it won't generate the geckodriver.log file:
driver = webdriver.Firefox(options=options, service_log_path=None)
I know this is old but this is still the first thing that comes up when you search for a way to prevent logging from selenium and it did not get rid of the "dev listening" messages for me and I found a way that does:
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
IWebDriver driver = new ChromeDriver(service,options);

Categories