2017-09-12 14 views
0

login.phpパス値

<script type="text/javascript"> 
    function validate(){ 
    var res=confirm("Are you sure"); 
    if(res) 
     return true; 
    else 
     return false; 
    } 
</script> 

を提出したときに、私はセレンを使用してPythonスクリプトからログインしようとしています次の関数を呼び出します。ログインをクリックすると、次のスクリプトが呼び出され、ログインするには「はい」を選択する必要があります。

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

browser = webdriver.Firefox() 
browser.get("webpage/login.php") 

username = browser.find_element_by_id("id") 
password = browser.find_element_by_id("pass") 
username.send_keys("username") 
password.send_keys("password") 
login_attempt = browser.find_element_by_xpath("//*[@type='submit']") 
login_attempt.submit() 

#I want to send yes to the `validate` function and the 
#then then again do browser.get("webpage/home.php") to scrape info from a html tag 

#print(login_attempt.submit()) 

私は、私がやろうとしていることに対して比較的新しいです。より良い方法があれば、提案がよく受けられます。

+0

すでに投稿された回答を試してください。 'https://stackoverflow.com/questions/46105357/how-to-handle-new-poped-up-browser-window-with-java-selenium/46109119#46109119' –

+0

[this ](https://stackoverflow.com/a/29052586/3989888)は便利だが、 'xpath(u '// input [@ value =" OK "]')。 。だから 'window.confirm'の' Ok'ボタンの要素の型は何ですか12. –

答えて

1

クリックを送信した後にポップアップが表示され、JSアラート/確認と呼ばれ、JavaScriptによってトリガーされ、ブラウザによってネイティブにサポートされます。そのようなポップアップのために、セレンはすでにそれらと相互作用するコードを実装していました。だからちょうどあなたが使用したバージョンでセレニウムのPythonのクライアントAPIからのMethondを見つける。 (異なるクライアントAPIバージョンでは、同じ名前と同じ方法で呼び出すことはできません)。

alert = browser.switch_to_alert() 
//accept the alert 
alert.accept() 

// maybe as below to call 
alert = driver.switch_to.alert 
alert.accept() 

// or 
Alert(driver).accept() 
+0

'selenium.common.exceptions.WebDriverException:メッセージ:スクリプトを文字列に変換できませんでした。 ' –

+0

この記事をご覧ください:https://seleniumwithjavapython.wordpress .com/selenium-with-python/handling-javascript-alerts/ – yong

+0

http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.alert – yong

0

利用コード以下:

browser = webdriver.Firefox() 
browser.get("http://username:[email protected]/login.php") 

あなたはこの認証の警告を回避することができるようになります。

+0

それは動作しませんでした.. –