python - 如何使用幻像和基本 http 身份验证调试 selenium webdriver?

标签 python selenium phantomjs

我正在尝试使用 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/

相关文章:

selenium - 是否可以使用页面对象为多个类似屏幕创建可重用且通用的 Specflow 步骤定义?

javascript - 如何使用 php-phantomjs 在 headless chrome 中执行 Javascript?

python - 如何删除从 csv 导入的 python pandas 数据框中的部分数据?

python - Selenium - Firefox 的 MoveTargetOutOfBoundsException

javascript - 在 python 中使用 selenium,如何从 HTML 中获取在 JS &lt;script&gt; 元素中声明的 Var

ruby-on-rails-3 - Cucumber + Capybara + Selenium firefox 打开 example.com 而不是我的应用

javascript - Phantomjs 无法正确捕获网页的屏幕截图

javascript - 幻影 Node 未指定默认引擎

python - pytesseract 提高了图像上模糊数字的 OCR 准确性

python - 我该怎么做才能让 python 使用补码来解释位,就像 c 一样