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

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

Related

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 can I test a remember me feature using selenium 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")

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

Selenium with Python, how do I get the page output after running a script?

I'm not sure how to find this information, I have found a few tutorials so far about using Python with selenium but none have so much as touched on this.. I am able to run some basic test scripts through python that automate selenium but it just shows the browser window for a few seconds and then closes it.. I need to get the browser output into a string / variable (ideally) or at least save it to a file so that python can do other things on it (parse it, etc).. I would appreciate if anyone can point me towards resources on how to do this. Thanks
using Selenium Webdriver and Python, you would simply access the .page_source property to get the source of the current page.
for example, using Firefox() driver:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.example.com/')
print(driver.page_source)
driver.quit()
There's a Selenium.getHtmlSource() method in Java, most likely it is also available in Python. It returns the source of the current page as string, so you can do whatever you want with it
Ok, so here is how I ended up doing this, for anyone who needs this in the future..
You have to use firefox for this to work.
1) create a new firefox profile (not necessary but ideal so as to separate this from normal firefox usage), there is plenty of info on how to do this on google, it depends on your OS how you do this
2) get the firefox plugin: https://addons.mozilla.org/en-US/firefox/addon/2704/ (this automatically saves all pages for a given domain name), you need to configure this to save whichever domains you intend on auto-saving.
3) then just start the selenium server to use the profile you created (below is an example for linux)
cd /root/Downloads/selenium-remote-control-1.0.3/selenium-server-1.0.3
java -jar selenium-server.jar -firefoxProfileTemplate /path_to_your_firefox_profile/
Thats it, it will now save all the pages for a given domain name whenever selenium visits them, selenium does create a bunch of garbage pages too so you could just delete these via a simple regex parsing and its up to you, from there how to manipulate the saved pages

Categories