我正在尝试使用 selenium webdriver (python) 和 phantomJS 来自动化在特定网站上完成的一些无聊的管理工作。该网站有一个基本的http auth 机制。我运行以下代码,但我没有得到任何东西(即 screen.png 或标题中没有任何东西)。当我使用相同格式( https://username:password@mywebsite.com )使用 wget 连接时,它可以工作,并且我得到了成功登录时应显示的初始主页。我该如何调试这个..有没有办法打开详细信息,以便我可以看到真正的情况。另请注意,不能选择使用 Firefox 或任何其他 UI Web 驱动程序,因为我只想从防火墙内的 Linux 盒子运行它。
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
import getpass
login = "myname"
password = getpass.getpass("Enter password")
password = password.replace("@","%40")
url = "https://%s:%s@mywebsite.com/"%(login,password)
print url
# Create a new instance of the Firefox driver
driver = webdriver.PhantomJS('./phantomjs/bin/phantomjs')
# go to the website
driver.get(url)
driver.save_screenshot('screen.png')
# the page is ajaxy so the title is originally this:o
print driver.title
driver.set_window_size(1024, 768)
driver.get_screenshot_as_png()
谢谢
最佳答案
作为变体:
- 将
password.replace()
更改为urllib.quote()
,因为密码中可能有多个@
特殊符号。< - 将 PhantomJS 驱动程序更改为 Firefox(或任何其他浏览器),您就会看到 PC 上到底会发生什么。
- 在
driver.get()
之后,使用之前导入的 EC 来等待页面上的某些元素,并且仅在此之后 - 截取屏幕截图。
关于python - 如何使用幻像和基本 http 身份验证调试 selenium webdriver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24621945/