2016-05-19 2 views
5

私はSelenium Webdriverでいくつかのunittestを実行しています。Seleniumとphantomjs webdriverを使って基本認証(1回のクリック)を正しく渡す方法

私は成功しwebdriver.Firefox()を使用して実行されます全体のテストを持って、ここでの設定は次のとおりです。

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 

テストが正常に実行され、しかし、私は手動でテストするための基本的な認証を複数回に入力する必要が前進し続けるここでは、(各クリックで基本認証を渡すことができますようにそれは音としての基本的な認証をバイパスし、真に自動化された全体のテストを持ってしようとする試みで、私は、phantomjsへのFirefoxから切り替えている

は私の設定です

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 

しかし、私は次のエラーを取得する:phantomjsに切り替えた後にこのエラーがphantomjsの間だけの違いである場合

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"}} 

私はわかりませんファイアフォックス、または私が間違って認証を渡すだけの場合。ここで

答えて

4

はPhantomJSと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://...") 
+0

おかげで、これは、すべての連続したクリックでauthentication_tokenを渡すのだろうか? – david

+0

クリックが新しいページへのリンクであるが、私はAjax呼び出しが不明です。 –

+1

私はあなたの答えを受け入れましたが、基本認証が複数のホストからリクエストされているため(ページ内の埋め込みコンテンツには独自の基本認証が必要です)、これで問題は解決しませんでした。私はSeleniumを使用してこのように複数のホストに資格情報を渡す方法があるかどうか分からないが、私はphantomjsが回避策を提供すると思ったが、そうは思わない。 – david

関連する問題