如何使用Selenium和phantom.js Webdriver正确传递基本身份验证(每次单击) [英] How to correctly pass basic auth (every click) using Selenium and phantomjs webdriver

查看:217
本文介绍了如何使用Selenium和phantom.js Webdriver正确传递基本身份验证(每次单击)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Webdriver运行一些单元测试.

I am running some unittests with Selenium Webdriver.

我有一个完整的测试,可以使用webdriver.Firefox()成功运行,这是安装程序:

I have an entire test that runs successfully using webdriver.Firefox(), here is the setup:

def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(30)
    self.base_url = "http://www.nike.com"
    self.verificationErrors = []
    self.accept_next_alert = True

测试成功运行,但是我必须多次手动输入基本身份验证才能继续进行测试.

The test runs successfully, however I have to manually enter in basic auth multiple times for the test to keep moving forward.

为了绕过基本身份验证并使整个测试真正自动化,我已经从Firefox切换到了phantomjs,因为听起来您可以通过每个click()传递基本身份验证

In an attempt to bypass basic auth and have the entire test truly automated, I have switched from Firefox to phantomjs, as it sounds like you can pass in Basic Auth with each click()

这是我切换到phantomjs后的设置:

Here is my setup after switching to phantomjs:

def setUp(self):
    dcap = dict(DesiredCapabilities.PHANTOMJS)
    dcap["phantoms.page.settings.userName"] = ("testuser")
    dcap["phantoms.page.settings.userPassword"] = ("testpass")
    self.driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=['--ignore-ssl-errors=true'])
    self.driver.implicitly_wait(30)
    self.base_url = "http://www.nike.com
    self.verificationErrors = []
    self.accept_next_alert = True

但是出现以下错误:

NoSuchElementException: Message: {"errorMessage":"Unable to find
element with xpath '(//button[@type='button'])[2]'","request":
{"headers":{"Accept":"application/json","Accept-Encoding":"identity",
"Connection":"close","Content-Length":"113","Content- Type":"application/json;charset=UTF-8",
"Host":"127.0.0.1:58025","UserAgent":"Pythonurllib/2.7"},"httpVersion":"1.1",
"method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"c2fa02e0-1df0-11e6-a2ad-c325e56df16d\",
\"value\": \"(//button[@type='button'][2]\"}","url":"/element","urlParsed":
{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"",
"password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":    
["element"]},"urlOriginal":"/session/c2fa02e0-1df0-11e6-a2ad-c325e56df16d/element"}}

我不确定这个错误是否只是phantomjs和firefox之间的区别,还是我只是错误地传递了auth.

I am not sure if this error is just a difference between phantomjs and firefox, or if I'm just passing auth incorrectly.

推荐答案

以下是使用PhantomJS和Selenium在标头中定义基本身份验证令牌的方法:

Here is the way to define the basic authentication token in the headers with PhantomJS and Selenium:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import base64

authentication_token = "Basic " + base64.b64encode(b'username:password')

capa = DesiredCapabilities.PHANTOMJS
capa['phantomjs.page.customHeaders.Authorization'] = authentication_token
driver = webdriver.PhantomJS(desired_capabilities=capa)

driver.get("http://...")

这篇关于如何使用Selenium和phantom.js Webdriver正确传递基本身份验证(每次单击)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆