Selenium: Driver ignores custom profile when headless mode is enabled - python

I have the following code:
options = webdriver.ChromeOptions()
options.add_argument('log-level=3')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("--user-data-dir=C:\\users\\eirik\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("--profile-directory=Bot")
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(executable_path="C:\\Program Files\\chromedriver.exe", options=options)
This code functions properly, however, If I were to add options.add_argument("--headless") the browser will work as expected with the exception of the custom profile being completely ignored. So browser presets, such as logins and extensions are missing.
I'd be glad for any help!
Kind Regards
-Eirik

Related

How to skip the DEBUG logs with in the console using Selenium and Python

I'm using Selenium with python.
and I'm getting these in the output:
Which I want to skip.
So please tell me how can I skip these warnings.
To suppress the DEBUG logs you can use add_experimental_option() through an instance of Options() as follows:
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(executable_path=r'C:\BrowserDrivers\chromedriver.exe', options=options)
driver.get("https://www.google.com/")

Specific sites doesn't render using Selenium (python)

I've been using selenium with python on both Chrome and Firefox. This specific website stays blank on both browsers when I try to run it with selenium - I'd appreciate any help. Here's my code for chrome:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("excludeSwitches",["enable-automation"])
path = r'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(executable_path=path, chrome_options=options)
driver.get('https://main.knesset.gov.il/Activity/committees/pages/allcommitteesagenda.aspx')
Add the argument --disable-blink-features=AutomationControlled
Code Block:
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(executable_path=r'C:\BrowserDrivers\chromedriver.exe', options=options)
driver.get("https://main.knesset.gov.il/Activity/committees/pages/allcommitteesagenda.aspx")
Browser Snapshot:
References
You can find a couple of relevant detailed discussion in:
How to evade blocking by Walmart using Selenium Webdriver
Selenium can't open a second page

chrome in headless mode with selenium

How to open chrome in headless mode with selenium? I tried
chromeOptions = Options()
chromeOptions.add_argument("headless")
self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=chromeOptions)
but this just returns an error
Error with Permissions-Policy header: Unrecognized feature: 'interest-cohort'.", source: (0)
This is wrong way
chromeOptions.add_argument("headless")
try this
chromeOptions.add_argument("--headless")
I am using these config in my project, so thought it would be helpful to you.
options = webdriver.ChromeOptions()
options.add_argument("--disable-infobars")
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
options.add_argument('--window-size=1920,1080')
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path = driver_path, options = options)
Note that driver_path is basically the chromedriver.exe, if ChromeDriverManager().install() works for you better stick with that.
headless is not a correct command format. Chrome command line switch always starts with -- that means it should be --headless
--headless: Run in headless mode, i.e., without a UI or display server dependencies.
Code:
chromeOptions = Options()
chromeOptions.add_argument("--headless")
self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=chromeOptions)
Please refer the below page for all command line switches and their details,
Reference: https://peter.sh/experiments/chromium-command-line-switches/
i'm using undetected_chrome, i used the below code to do mine and it worked perfectly
options = webdriver.ChromeOptions()
options.add_argument("--disable-infobars")
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
options.add_argument('--window-size=1920,1080')
options.add_argument("--headless")
driver = uc.Chrome(use_subprocess=True, options=options)

Selenium Chrome driver not generating search results in headless mode

I was doing some scraping task and came across this issue where, when the browser is made to run in headless mode, it does not produce search results, rather gives a blank body.
This issue is not encountered in every run, but randomly pops ups in between some runs.
Expected:
Actual (image saved by driver.save_screenshot()):
configuration of webdriver:
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument("--disable-extensions")
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument("--start-maximized")
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-running-insecure-content')
options.add_argument("--window-size=1920,1080")
While in headless mode only this issue is being faced.
Methods already tried include:
Implicit wait
time.sleep()
Browser reload
Environment: MacOS 11.5.2
Any help is appreciated

How to disable file download popup in firefox using selenium and python?

Im trying to download some reports from web page, using selenium and python
when i click on download link firefox shows save/open dialog
I have used firefox profile configuration as follows
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile", "application/pdf,attachment/pdf")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,attachment/pdf")
profile.set_preference("browser.download.dir", "c:\\firefox_downloads\\")
self.browser = webdriver.Firefox(profile)
but still it shows message box,
i have changed
self.browser = webdriver.Firefox(firefox_profile=profile)
like above but no chance >
Can any one help me to resolve this?
I have a little helper function which works for me.
It uses chromedriver though.
def driver_download(location_for_download):
# options = Options()
# options.headless = True
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': location_for_download}
chrome_options.add_experimental_option('prefs', prefs)
# driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=chrome_options)
return driver
Note: For my particular application, I was unable to use the headless. But it should work if its a direct link to the file.

Categories