簡體   English   中英

在 Python 和 Selenium WebDriver 中打開受密碼保護的頁面

[英]Opening a password protected page in Python and Selenium WebDriver

我試圖在我的網絡瀏覽器中打開這個本地 IP。

   from selenium import webdriver
    driver = webdriver.Firefox()
    url = 'http://165.269.261.210/source/'
    page = urllib.urlopen(url)

當我運行上面的代碼時,它要求在 Python shell 上輸入用戶名和密碼。

Enter username for coffee at 165.269.261.210: agrawal

Warning (from warnings module):
  File "C:\Python27\Lib\getpass.py", line 92
    return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Enter password for agrawal.a in coffee at 165.269.261.210: bravokid

在我提供用戶名和密碼后,Firefox 中沒有任何打開? 以及如何在代碼本身中提供用戶名和密碼? 有沒有更好的方法來做到這一點?

更新:當我直接將鏈接放入瀏覽器時,會打開一個彈出窗口,要求輸入用戶名和密碼。 只有在我提供用戶名和密碼頁面后才會打開,否則會引發 401 錯誤

你在這里做什么:

from selenium import webdriver
driver = webdriver.Firefox()
url = 'http://165.269.261.210/source/'

# This is no way connected to Selenium, but fetches URL using Python urllib library
# page = urllib.urlopen(url)
driver.get('http://username:password@165.269.261.210/source/')

相反,請按照 Selenium WebDriver 教程中的說明使用driver.get()方法。

此外,您需要在 URL 中為 HTTP Basic Auth 傳遞用戶名和密碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM