how can I test a remember me feature using selenium python - python

how can I write a test case validation for a remember me button as selenium every time opens a new instance of the browser with no cookies.
I heard ChromeOptions class can store cookies in a private file.
I tried searching for the argument and found-
"user-data-dir=/path/"
how can I use this path back into my next test case in order to validate it?

Add this code in your second loop
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\YOUR_USERNAME_HERE\\AppData\\Local\\Google\\Chrome\\User Data\\")
You may or may not then also need to re-instantiate the driver
driver = webdriver.Chrome(PATH, options=options)
and do the GET request
driver.get("http://127.0.0.1:8000/route_that_requires_remember_me")

Related

start Selenium chrome driver minimized (or equivalent)

I'm making a web scraping program, but to keep from being blocking by anti-scraping software I gotta keep the driver out of headless mode. sometimes I also need to restart the driver to clear the cookies, so whenever it opens back up I minimize it immediately, but it still gets in the way of whatever Im doing for about a second, and this program runs for hours so its incredibly annoying
Im thinking theres probably something I can add like driver.add_option("start in minimized") or driver.add_option("start in off screen") (equivalent to driver.set_window_position(-2000,0) but before the driver opens
does anyone know of an options setting I can add for this?
heres what my current code looks like, though this is more of a feature question than a bug fixing problem
import undetected_chromedriver.v2 as uc
def start_uc():
'''opens a UC chrome driver with prefered settings'''
options = uc.ChromeOptions()
# just some options passing in to skip annoying popups
options.add_argument('--no-first-run --no-service-autorun --password-store=basic')
return uc.Chrome(options=options)
driver = start_uc()
driver.minimize_window()
for item in list:
#scrape item info
if blocked from page:
driver.close()
driver = start_uc()
driver.minimize_window()
Unfortunately there is no such option / capability.
You can set driver to start maximized with
options.add_argument("start-maximized")
But there is no such option as options.add_argument("start-minimized").
As you mentioned, you can minimize the driver screen immediately after it's creating with driver.set_window_position(-2000,0) but, again, this will be applied only after the driver is opened.

How can I get Selenium to operate directly on an existing Firefox profile?

I've discovered that even if I specify:
webdriver.Firefox(
firefox_profile=webdriver.FirefoxProfile("/my/profile/path")
)
Selenium actually clones that profile and launches the browser based on that temporary clone. I'm sure this makes sense when you're using Selenium for testing, but as I'm using it for automation, I'd like to operate directly on the existing profile.
Is this possible with Selenium? Really I'm looking for a way to maintain state between sessions (bookmarks, history, Firefox sync setup, etc.) so a persistent session makes the most sense to me. I'm doing this work in Python, but I imagine that the pattern used in other languages would be similar.
Try this
from selenium import webdriver
myprofile = webdriver.FirefoxProfile(r'C:\Users\{YOUR OWN USERNAME}\AppData\Roaming\Mozilla\Firefox\Profiles\moskcpdq.SeleniumTest')
driver = webdriver.Firefox(firefox_profile=myprofile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()
Make sure you are using the correct path for the profile!

How to give a variable for user-data-dir in selenium?

I am using Selenium Chrome driver using Python for some automation activities. I need to remember the logins and everything next time I'm starting the code and I'm using the options with user-data-dir for this.
options = Options()
options.add_argument("user-data-dir=cookiename")
driver = webdriver.Chrome(options=options)
driver.get("site name")
Here is how my code looks like. The first time I'm running it, a directory named cookiename gets created.
What I want is to set a variable instead of cookiename, for eg:
cookiename = cookiefolder
options.add_argument("user-data-dir=cookiename")
and the folder created should be cookiefolder. Is it possible? I can't implement it like this.
This existing method is kind of hardcoding isn't it?
It was actually simple,
cookiename=cookiefolder
options.add_argument("user-data-dir="+cookiename)
basic python stuff

how to search for string inside chrome internal pages, using python and\or selenium

I'm trying to write script, that would allow me to search for a string inside Chrome's internal pages (for example: "chrome://help").
is there a simple way to do it, or does it require special tools, (like the Selenium webdriver API)?
i know it's possible to use it for "normal" web pages, but what about "internal" ones?
You can use selenium Webdriver to easily achieve this task, and in this case we will extract the version number of Google Chrome.
Here is the sample code with comments explaining every step:
from selenium import webdriver
# executable path should be the place where chrome driver is on the computer
driver = webdriver.Chrome(executable_path= '/users/user/Downloads/Chromedriver')
# This line tells the driver to go to the help section of chrome
driver.get('chrome://help')
# Because certain elements are stored in another Iframe, you must switch to this particular Iframe which is called 'help' in this case.
driver.switch_to.frame(driver.find_element_by_name('help'))
# retrive the text of the element and store it's text in a variable.
version_string = driver.find_element_by_id('version-container').text
# Now you can easily print it.
print version_string

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

Categories