7

I am trying to enter data in prompt (URL Given), below codes is giving me an error. Please help me out with these?

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
url = "http://the-internet.herokuapp.com/basic_auth"
driver.get(url)
time.sleep(5)
alert = driver.switch_to.alert
alert.authenticate('admin','admin')
time.sleep(4)
alert.accept()

I have tried with:

ActionChains(driver).send_keys("admin").send_keys(Keys.TAB).send_keys("admin").perform()

This one is also not working.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
jaibalaji
  • 3,159
  • 2
  • 15
  • 28
  • Not sure if this is your problem but the first thing I see is you forgot the "()" in alert = driver.switch_to.alert() – Chai Jul 26 '17 at 13:48
  • tried : `alert = driver.switch_to.alert() TypeError: 'Alert' object is not callable` – jaibalaji Jul 26 '17 at 13:53

3 Answers3

8

When you work with Selenium 3.4.0, geckodriver v0.18.0, Mozilla Firefox 53.0 through Python 3.6.1 you can bypass the Basic Authentication popup through embedding the username and password in the url itself as follows.

This solution opens the URL http://the-internet.herokuapp.com/basic_auth and authenticates with a valid username and password credentials.

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • FWIW: embedded credentials will only partially work using chrome v59+ because sub-resource requests will be blocked. – orde Jul 26 '17 at 18:34
1
    def test_1_authentication(self):
        self.driver.get("https://the-internet.herokuapp.com/basic_auth")
        shell = win32com.client.Dispatch("WScript.Shell")
        shell.Sendkeys("admin")
        time.sleep(3)
        shell.Sendkeys("{TAB}")
        time.sleep(3)
        shell.Sendkeys("admin")
        time.sleep(3)
        shell.Sendkeys("{ENTER}")
        time.sleep(3)

Above code is also properly worked :)

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
jaibalaji
  • 3,159
  • 2
  • 15
  • 28
  • Is the above code working now? I found the same piece of code which was working well for me is not working now. I asked question about the same here https://stackoverflow.com/questions/49491048/windows-authentication-code-no-longer-working-now – Shoaib Akhtar Apr 03 '18 at 10:26
0

You can also use PyAutoGui:

pip3 install pyautogui
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import pyautogui


driver = webdriver.Chrome()
driver.get("htt`enter code here`p://95.141.198.238/noload2/files/074/UTF- 
RSLOAD.NET-.zip")

pyautogui.write('rsload.net') # login
pyautogui.press('tab')        # Tab
pyautogui.write('rsload.net') # pass
pyautogui.press('enter')      # Enter

    
S.B
  • 13,077
  • 10
  • 22
  • 49
Sergy
  • 1