How to send HTTP authentication using Selenium python-binding webdriver

I am using Selenium python binding to set up an automation test for our web application. I ran into a problem while testing a web server on a beta server because it requires HTTP authentication for authentication on the intranet and password.

from selenium import webdriver driver = webdriver.Firefox() driver.get("https://somewebsite.com/") 

I need to send a username and password for a popup dialog when accessing http://somewebsite.com/

Is there a neat way to do this?

+6
source share
2 answers

I found a solution to this question:

 from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference('network.http.phishy-userpass-length', 255) driver = webdriver.Firefox(firefox_profile=profile) driver.get("https://username: password@somewebsite.com /") 

Part of FirefoxProfile should reject the confirmation dialog, because by default Firefox will show a pop-up dialog to prevent it from being sent.

+7
source

Another solution:

log in with python requests, get cookies and click cookies to selenium browser

 import requests from selenium import webdriver from requests.auth import HTTPBasicAuth session = requests.Session() www_request = session.get('http://example.com', auth=HTTPBasicAuth('username','password'), allow_redirects=False) driver = webdriver.Remote(...) #chrome needed to open the page before add the cookies driver.get('http://example.com') cookies = session.cookies.get_dict() for key in cookies: driver.add_cookie({'name': key, 'value': cookies[key]}) driver.get('http://example.com')
import requests from selenium import webdriver from requests.auth import HTTPBasicAuth session = requests.Session() www_request = session.get('http://example.com', auth=HTTPBasicAuth('username','password'), allow_redirects=False) driver = webdriver.Remote(...) #chrome needed to open the page before add the cookies driver.get('http://example.com') cookies = session.cookies.get_dict() for key in cookies: driver.add_cookie({'name': key, 'value': cookies[key]}) driver.get('http://example.com') 
0
source

Source: https://habr.com/ru/post/894794/


All Articles