I encountered an issue with chrome when using selenium with python.
When the script runs the first time it works but after It won't.
The cached data have some issues, When I try to delete the cache in settings, it shows "Cached images and files
Calculating …" and keeps loading forever.
Also some websites don't work like WhatsApp web.
I discovered a workaround,
is to delete the cache directory under the Default directory and reopen chrome.
So I set my script to delete this directory before launching chrome, but I encounter a lot of errors (Permission denied, etc..), Or using the --disable-application-cache=0 option .
But this workarounds aren't best practices.
Is there any fix to this issue? (I'm sure the problem isn't in my selenium script because the bug stills even if I launch chrome by myself).
Here's my code:
def __init__(self, wait, session=None,headless=False,logging_in=False):
chrome_options = Options()
chrome_options.add_argument("--lang=en-US")
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36")
if session:
chrome_options.add_argument("--user-data-dir={}".format(os.path.abspath(session)))
chrome_options.add_argument("--profile-directory=Default")
if headless and os.path.exists(os.path.abspath(session)) and not os.path.exists(os.path.join(os.path.dirname(__file__),"not_logged.in")):
chrome_options.add_argument("headless")
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])
self.browser = webdriver.Chrome(options=chrome_options) # we are using chrome as our webbrowser
else:
self.browser = webdriver.Chrome()
self.browser.get("https://web.whatsapp.com/")
I can add this to solve the issue:
chrome_options.add_argument("--disable-application-cache=0")
This disables caching which I don't want to do especially for WhatsApp.
The problem is that there is multiple instances of chromedriver.exe using the same user directory, wich causes the cache loading issue, so you need to ensure that you quit from every session you start (Even if the script fails) using:
driver.quit()
Related
My selenium script is not working on headless mode but working fine without headless. Error msg (NoSuchElementException)?why?
Increase time.sleep 2 to 10 but still not working
Some of the applications do not work properly in the headless mode due to the application firewall. In that case, you need to debug what is showing inside headless.
Take a snapshot using selenium and see what is showing before the element
not found exception
If you found your application is blocked to load in headless then follow the steps
To bypass the firewall you can use following argument in your python script. I used it in my java so i mention it in java. You can convert to your desire language
options.addArguments("--headless");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36");
I am using Linux Mint 20. I am using User-Agent Switcher and Manager and Spoof Timezone with firefox. I want to load the current firefox user profile and use Chrome 99.0.7113.93 (Windows) user agent using selenium. In addition to that, when right click on Spoof Timezone, there is an option Update timezone from IP, I also want to click that before going through rest of the process.
Currently I am following save document.cookie output in a file and came up to:
driver = webdriver.Firefox(
executable_path=GeckoDriverManager(cache_valid_range=1).install())
driver.get('https://www.skillshare.com/')
cookie = driver.execute_script('return document.cookie')
f = open("/home/blueray/Desktop/cookie.txt", "w")
f.write(cookie)
f.close()
driver.close()
How can I do that?
Unfortunately you cant exactly click on the on the extensions with selenium, since they are not part of the page DOM.
For User-Agent Switcher and Manager you can just inject the user agent without using the extension
For Spoof Timezone, You can access the about:addons, click on the extension, preferences, check the automatically update timezone based on my IP address and click save. Cant do it with selenium since that part is under a shadowroot that doesnt display these settings. Hopefully when the selenium launches you will have the setting already saved, after you've done this step manually.
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
options = webdriver.FirefoxOptions()
# Define and set the user agent to Chrome 99;
# The User-Agent Switcher and Manager extension has no config page that we can access as an url and click on it with selenium;
# Therefore we can injecting the user agent instead
user_agent = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.7113.93 Safari/537.36"
options.set_preference("general.useragent.override", user_agent)
# load the firefox profile
# to get this: open in firefox the url about:profiles and its the Profile: default-release => Root Directory
firefox_profile = FirefoxProfile('/home/art/.mozilla/firefox/kd5i4tgp.default-release')
options.profile= firefox_profile
firefox_profile.add_extension("/home/art/.mozilla/firefox/kd5i4tgp.default-release/extensions/{55f61747-c3d3-4425-97f9-dfc19a0be23c}.xpi") # for spoof timezone
#download from here https://github.com/mozilla/geckodriver/releases/tag/v0.29.1 linux64.tar.gz, I've put mine in Documents
driver = webdriver.Firefox(options=options,executable_path="/home/art/Documents/geckodriver")
driver.get('https://www.skillshare.com/')
cookie = driver.execute_script('return document.cookie')
f = open("/home/blueray/Desktop/cookie.txt", "w")
f.write(cookie)
f.close()
driver.close()
So I have been trying to access my gmail or Google Colab Notebooks through selenium.
Instead of authenticating through Email and Password I am starting the chromedriver with an already saved Google Chrome Profile, but it throws this:
I have tried every single solution that used to work before but it ain't working now, some of the solutions that I tried are:
Disabling Two Factor authentication
Allowing Less secure app access
Disabling every single flag that I know that lets websites detect that website is being controlled by a bot.
Trying to login using some other site's Google OAuth.
Trying different Viewports
The user agent is also the same that is used when logging in without selenium manually.
options = Options()
options.add_argument('--user-data-dir=/home/mubbashir/.config/google-chrome')
options.add_argument('--profile-directory=Default')
options.add_argument("--disable-blink-features")
options.add_argument('--disable-web-security')
options.add_argument('--allow-running-insecure-content')
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--start-maximized')
options.add_argument('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36')
Nothing works at all! I am kinda stuck and any help would be very much appreciated.
Thank you so much!
For anyone facing this, the easiest way is to use cookies.
Download Colab's Cookies for that particular account.
In selenium, add them to your webdriver first and then go to the notebook link.
I am trying to login to my e-trade account via selenium and/or chrome_driver, but each time I attempt to login e-trade seems to be able to detect that I am using a webdriver and block the login. Is there any way for me to point my chrome driver to my normal chrome session? or prevent etrade from detecting that I am using one of these drivers? I have read a few other SO posts that have suggested some workarounds, but so far nothing has worked. I have tried both webdrivers on chrome and firefox, also tried clearing my cookies before login.
I am on ChromeDriver 76.0.3809.126 and Selenium server version: 3.141.59, any suggestions to get around this would be greatly appreciated.
I use python asyncio and pyppeteer with chrome to automate the login into etrade. A few months ago etrade changed something and I started getting blocked. With headless set to False I could see an error warning that stated to call etrade with the IP address. After a long back and forth they sent me a set of cookies (see below) that needed to be passed with a customer value specific to my account. Once they gave me the ETWLCUST1 value for the value key everything was golden from there on out. This isn't documented on the site so it was painful to figure out, but it worked for me. This may be what is blocking your access. I also passed user-agents prior to whatever etrade changed, and I still do, so that wasn't the issue for me.
Good luck if it works!
cookies = {'name':'SWH','value':'ETWLCUST1-xxxxxxx-xxxx','domain':'.etrade.com','secure':True,'httpOnly':True}
To achieve this you need to use the chrome User-Agent command-line option:
As you have not mentioned which language you are using, I am writing code in Java and Python.
To achieve this you need to use the chrome user-agent command line option.
JAVA:
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-agent="+ PUT_USER_AGENT_HERE);
WebDriver driver = new ChromeDriver(options);
PYTHON:
opts = Options()
opts.add_argument("user-agent=PUT_USER_AGENT_HERE")
driver = webdriver.Chrome(chrome_options=opts)
How to get the User-Agent:
Open your actual browser.
Right-click and open inspect element.
Now go to console.
Now copy-paste below code.
Code:
navigator.userAgent
Example: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
According to this Google does not support logging in with automation testing frameworks. I am trying to populate a calendar with game release data I scraped from here to make a public calendar I can share on reddit. I have scraped the data but now i can't even log into Google on the webdriver. What are the solutions to this?
driver = webdriver.Chrome()
driver.get("https://calendar.google.com/calendar/r")
pdb.set_trace()
#manual log in gives error
Error message: You are trying to sign in from a browser or app that doesn't allow us to keep your account secure.
Try using a different browser Learn More
There is some hack or workaround for this by setting the User-Agent in Chrome.
To achieve this you need to use the chrome user-agent command line option.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
opts.add_argument("user-agent=PUT_USER_AGENT_HERE")
driver = webdriver.Chrome(chrome_options=opts)
driver.get("https://calendar.google.com/calendar/r")
pdb.set_trace()
#manual log in gives error
How to get the User-Agent:
Open your actual browser.
Right-click and open inspect element.
Now go to console.
Now copy-paste below code.
Code:
navigator.userAgent
Example: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36