0

how can I login to an application using Selenium webdriver without hard-coding the password in the code?

Plus I want to hide the 'geckodriver.exe window' during execution.

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.keys import keys
import getpass

driver = webdriver.Firefox()
driver.maxmimize_window()

driver.get("https://example.com")
time.sleep(10)

username=driver.find_element_by_id("txtUserID")
username.clear()
username.send_keys("abc@xyz.com")

.....the rest of the code will continue....

  • I have added an answer for that please let me know your feedback for further assistance – Mahmud Riad Dec 12 '17 at 08:50
  • you can pass the password in as a command line argument, or have the script prompt the user for the password at runtime, or put the password in an environment variable. – Bryan Oakley Dec 12 '17 at 09:48
  • yes...but my code will launch the application's login page where the username and password will be prompted. From an end user point of view it is not advisable to ask them to add the username and password during runtime. – Sukanya Param Dec 12 '17 at 10:33

3 Answers3

0
  • Add an option for firefox headless . Than it will not be seen in the view.

     binary.add_command_line_options('-headless')
    

You will get the total procedure here

  • For not hardcoding the password you can create a config file and put the password or other configurations there. Then you can parse the password from that config file.

If you want to minimize the browser just apply below code

driver.manage().window().setPosition(new Point(-2000, 0));
Mahmud Riad
  • 1,169
  • 1
  • 8
  • 19
  • I would like to hide my geckodriver.exe window during code execution or is it bound to show up? – Sukanya Param Dec 12 '17 at 11:21
  • You want to minimize or you just dont want to see the execution window? headless option is executing testcases without view. – Mahmud Riad Dec 12 '17 at 11:22
  • @SukanyaParam Please check my latest answer. if you just want to minimize the browser which is already open just set the dimension of that browser. – Mahmud Riad Dec 12 '17 at 11:29
  • During execution, the geckodrive.exe cmd window opens and this has got nothing to do with the browser. So how should one hide the window? – Sukanya Param Dec 14 '17 at 09:47
0

xvfb is a common way of doing this. Searching for "selenium xvfb" should find lots, such as:

How do I run Selenium in Xvfb?

Is it possible to run Selenium scripts without having an X server running, too?

These will help you for headless firefox.

how can I login to an application using Selenium webdriver without hard-coding the password in the code?

For this you need to create one GUI or one screen from where user add the password and then you will get that value which was added by user and then use as a password in a specific website.

Hiten
  • 724
  • 1
  • 8
  • 23
  • Can I use it this way ? options = webdriver.firefox.options.Options() options.add_argument('headless') driver = webdriver.Firefox(options=options) – Sukanya Param Dec 12 '17 at 10:57
  • I tried this for firefox but it's not working. If you change your browser from firefox to chrome then it's like System.setProperty("webdriver.chrome.driver", "chromedriver"); ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("--headless"); driver = new ChromeDriver(chromeOptions); – Hiten Dec 13 '17 at 07:38
0

Another option is to use the open source Blinq.io, that copies the cookies of the login and then injects them in the automation test . Effectively it enables you to bypass the login of the application when doing test automation. The code is in github: https://github.com/blinq-io/selenium-session-manager/

Trex
  • 1
  • 2
    Welcome to Stack Overflow! Are you affiliated with the linked project? If so, please note that when linking to your own site or content (or content that you are affiliated with), you [must disclose your affiliation _in the answer_](/help/promotion) in order for it not to be considered spam. Having the same text in your username as the URL or mentioning it in your profile is not considered sufficient disclosure under Stack Exchange policy. – cigien Jul 05 '22 at 16:36