How to Submit HTTP authentication with Selenium python-binding webdriver

后端 未结 2 489
南旧
南旧 2020-12-03 15:34

I\'m using Selenium python binding to setup an automation test for our web application. I\'m facing a problem while testing the web on beta server because it requires HTTP a

相关标签:
2条回答
  • 2020-12-03 16:04

    I have 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/")
    

    The FirefoxProfile part is to dismiss the confirmation dialog because by default Firefox will show a popup dialog to prevent pishing.

    0 讨论(0)
  • 2020-12-03 16:08

    Another solution:

    login with python requests, get the cookies, and push the cookies into the selenium's 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')
    
    
    
    0 讨论(0)
提交回复
热议问题