如何调试Selenium WebDriver与Phantom及基本HTTP认证?
我正在尝试用selenium webdriver(python)和phantomJS来自动化一些在特定网站上进行的无聊的管理工作。这个网站有一个基本的http认证机制。我运行了以下代码,但没有得到任何结果(也就是说,screen.png和标题都没有内容)。当我用wget以相同的格式连接时(https://username:password@mywebsite.com),它可以正常工作,并且我得到了成功登录后应该显示的初始主页。我该如何调试这个问题呢?有没有办法开启详细模式,这样我就可以看到它到底得到了什么?另外,请注意,我不能使用firefox或其他任何图形界面的webdriver,因为我只想在防火墙内的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()
谢谢
1 个回答
1
作为变体:
- 把
password.replace()
改成urllib.quote()
,因为密码里可能有多个@
这样的特殊符号。 - 把 PhantomJS 驱动换成 Firefox(或者其他浏览器),这样你就能看到在你的电脑上到底发生了什么。
- 在使用
driver.get()
之后,利用之前导入的 EC 来等待页面上的某个元素出现,等这个元素出现后再截图。