Selenium can't open chrome - selenium

I was trying to open the CoinGecko website in a different profile in Chrome, but when I ran the code, it didn't throw any errors and just ended without opening the browser. I was wondering if you could help me figure out what I might be missing?
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument(
r"--user-data-dir=<directory path here>"
) # e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
options.add_argument(r"--profile-directory=<Profile number here>") # e.g. Profile 3
driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()), options=options
)
# Set the URL of the website you want to open
url = "https://www.coingecko.com/"
# Open the website
driver.get(url)

Remove the line - options.add_argument("--headless"), this line will open the browser in headless mode, and the browse window will not be visible.

Related

selenium automation signing to google cloud throws networks error

I'm using selenium & selenium wire in my project. I'm writing flows to log in google cloud portals..
I enter my google cloud mail then, press on continue in Google sign in and then it log in to gcp.
I got some errors:
Request has invalid authentication credentials. Expected OAuth 2 access token, login cookie or other...
net:: ERR_PROXY_CONNECTION_FAILED
when I do the same flow manually without automation, with the same credentials, it works fine and no any network error.
my web driver
from seleniumwire import webdriver
from seleniumwire.webdriver import ChromeOptions
def test_gcp_flow():
options = ChromeOptions()
options.add_experimental_option("detach", True)
options.add_argument('--no-sandbox')
options.add_argument('--single-process')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--start-maximized")
options.add_argument('--auto-open-devtools-for-tabs')
options.add_argument('--log-level=2')
options.add_argument('--disable-features=IsolateOrigins,site-per-process')
options.add_argument("--ignore_ssl")
options.add_argument('--ignore-ssl-errors')
options.add_argument('--ignore-certificate-errors')
options.add_argument("--disable-extensions")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--dns-prefetch-disable")
options.add_argument('ignore-certificate-errors')
options.add_argument('disable-web-security')
options.add_argument('--allow-insecure-localhost')
driver = webdriver.Chrome(options=options)
driver.get('....any-hidden-url')
# more flow actions - then it open gcp portal
I added openssl.cnf (without this openssl, it shows me TLS ssl issue) to run it locally in my test using Pycharm:
openssl_conf = openssl_init
[openssl_init]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
Options = UnsafeLegacyRenegotiation
I tried to add some change the chrome options that added to selenium driver but nothing has changed, still same error.
I tried to use Firefox webdriver and it works good without any network issue.
Maybe it's any chrome cache issue? because some days ago it worked me with chrome..
what I'm expecting is that to sign in to gmail without network/token issues.
I can think of two possible options:
Google detects Selenium and prevents login with it for security reasons. Well selenium web driver injects recognizable javascript to the browser, and you can detect it using a simple script.
You have an authentication cookie in your browser that is not used with Selenium.
Each Selenium session initiates itself completely clean. You can save your cookies with pickle for instance and then inject it to Selenium web driver with Python. Although I would avoid it if you cant store it encrypted in a secure place and fetch it during run time (or else - at some point it will be stolen no matter how much you think you are secured).
By the way if its something that you really need - YOU are a paying customer, maybe google has an available solutions for this kind of need.
Good luck!
Try using selenium wire with an undetected chrome-driver.
pip install selenium-wire
pip install undetected-chromedriver
from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
if __name__ == "__main__":
options = {}
chrome_options = ChromeOptions()
chrome_options.add_argument('--user-data-dir=hash')
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-dev-shm-usage")
browser = Chrome(seleniumwire_options=options, options=chrome_options)
browser.get('your url')
... your rest of the code
I hope it resolves the issue.

Firefox profile and selenium

I am unable to open url .I have created a new profile switched to it and deleted all old profiles on firefox(ubuntu 22.04). Still it can't access the profile it seems. "Your Firefox profile cannot be loaded. It may be missing or inaccessible." How do I resolve this.
#importing webdriver from selenium
from selenium import webdriver
#selecting Firefox as the browser
#in order to select Chrome
# webdriver.Chrome() will be used
driver = webdriver.Firefox()
#URL of the website
url = "https://www.geeksforgeeks.org/"
#opening link in the browser
driver.get(url)

Let selenium display browser while in --headless

Is there a way to run selenium headless but have it display the window for example at the start of the application ? like:
show browser
login
do captcha
go --headless
execute tasks
Actually, this is possible.
You can peek into a headless browser by using the "--remote-debugging-port" argument in ChromeOptions. For example,
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--remote-debugging-port=9222') # Recommended is 9222
driver = webdriver.Chrome(options=chrome_options)
Then while Selenium is running, you can go to http://localhost:9222 in your regular browser to view the headless session. Your browser will display links to each tab Chrome has open, and you can view/interact with each page by clicking on the links. You'll also be able to monitor Selenium/Chrome as Selenium runs its test.
Edit: As of Chrome 100 or so, you now need to go to the link chrome://inspect to view the headless session in your browser while Selenium is running.
You'll need to do some extra configuration as well. Basically:
Check the box for "Discover network Targets"
Hit "Configure..."
Add the link "localhost:9222" to the list of servers
CREDITS: https://stackoverflow.com/a/58045991/1136132

How to run Selenium chromedriver headless while using a user profile

I have coded a script to perform some actions while logged into my youtube account, using my Chrome user data folder:
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=options)
driver.get(https://youtube.com)
driver.get_screenshot_as_file("screenshot.png")
thing = driver.find_element_by_xpath(
'someXpath')
thing.click()
It works perfectly without the line:
options.add_argument('--headless')
However, when "headless" is added as an option, the screenshot taken by driver.get_screenshot_as_file shows that chrome is prompting a login to a google account, indicating that the specified chrome profile is not being used.
If possible, I'd like to use both headless mode and use my chrome profile at the same time. Thanks for any help.

Selenium: Configure Firefox webdriver to not run in test mode

I'm using Selenium to automate a navigation in Google Meet website for a user who can't use the keyboard. However, Google Meet won't let me enter a meeting when using Chrome in test mode. If I configure Chrome webdriver to run as a regular browser, I can navigate on the website a little but eventually I can't enter a meeting at all. Here is the python code I use to initialize Chrome as a non-test browser:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ['enable-automation'])
browser = webdriver.Chrome()
browser.get("https://meet.google.com/some_meeting_id")
time.sleep(3)
txtName = browser.find_element_by_css_selector('#jd\.anon_name')
txtName.send_keys("user_name" + Keys.RETURN) // redirects to an error page
My next hope is to use Firefox, but when I load it from webdriver, it opens with an orange address bar, indicating that it is being run by a test tool. Is there a way to run Firefox in normal mode from Selenium (just as I did with Chrome) or, even better, is there any additional configuration I can do in Chrome webdriver to make this work?
May be try this to switch the headless mode on firefox to normal mode
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = False
driver = webdriver.Firefox(options=options)
driver.get("http://stackoverflow.com")