2016-05-03 22 views
1

私はpythonでselenium webdriverを使用してjspフォームにログインしようとしています。私はパラメータを掲示してログインしようとしていますが、そのようなことについては、フォームや身体タグを超えたものには到達できません。何が間違っているのですか?以下は私のコードであり、その下のページのソースである - それは非公開のウェブページであることから:selenium webdriver pythonを使用してxpathでjspフォームにログイン

from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 

def my_method(): 
    driver = webdriver.PhantomJS() 
    driver.get("https://<URL>.se:20443/snl/login.jsp") 

    password = driver.find_element_by_xpath("//input[@id='j_password']") 

    driver.close() 

my_method() 

ページのソース:

<!DOCTYPE html> 
<!-- WARNING : modifying login.jsp may affect the login layout for Mobile, Embedded and Desktop Version. --> 
<html> 
    <head> 
     <!-- Import header for script/style ... --> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <meta http-equiv="Cache-Control" content="no-cache"/> 
    <meta http-equiv="Expires" content="-1" /> 
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> 

    <link type="image/x-icon" rel="shortcut icon" href="resources/images/favicon.ico">  
    <link type="text/css" rel="stylesheet" href="resources/style/login.css"> 

    <style type="text/css"> 
    body{ 


      background: url(resources/images/gradient_body_login.png) repeat-x; 

     background-color: #d3d3d3; 

    } 

     #container{ 
      background: url(resources/images/background_body.png) no-repeat top; 
      width:1090px; 
      min-height:770px; 
      height:auto; 
     } 


     #container{ 
      margin:auto; 
      margin-top:0; 
     } 


    #login input[type="submit"],#login input[type="button"]{ 

     margin:10px 5px 10px 0; 

    } 

     #mask{ 

      opacity: 0.5; 
      filter: alpha(opacity = 50);    
     } 
    </style> 

    <script type="text/javascript" src="resources/script/live.js"></script> 
    <script type="text/javascript"> 

      <!-- Against XFS attack --> 
      if(top != self) 
       {top.location=self.location;}  
      <!-- Against XFS attack --> 


    function Start(){ 
     DisplayContent(); 
     GiveFocus("j_username"); 

     <!-- Check login failed --> 
     var vars = getUrlParameters(); 
     var loginLabel = document.getElementById("failureIndication"); 
     var authFailed = vars["authfailed"]; 
     if (authFailed === "true") 
      loginLabel.innerHTML = "Login Failed"; 
     <!-- Check login failed --> 
    } 

    function DisplayContent() { 
     var mainFrame = document.getElementById("hasJavascript"); 
     mainFrame.style.display = 'block'; 
     } 

    function GiveFocus(id){ 
     document.getElementById(id).focus(); 
    } 

    function setSubmitUrl(form){ 
     var hash = getUrlHash(); 
     if((hash != null) && (hash != "")) { 
      form.action = "j_spring_security_check#" + hash; 
     }else { 
      form.action = "j_spring_security_check"; 
     } 
     return true; 
    } 

    </script> 

    <!-- Sample of custom logo --> 
    <style type="text/css"> 
     h2 { 
      background: url("resources/large.png") no-repeat 20px 0 transparent; 
      line-height: 20px; 
      padding-left: 170px; 
      background-size: 75px 33px !important; 
      background-position: 35px 0px !important; 
     } 
     body { 
      font-family:Verdana;font-size:12px;color:#444;margin:0;padding:0;width:100%;height:100%; 
      background-color: #5BBF19 !important; 
      background-attachment: fixed !important; 
      background-repeat: no-repeat !important} 
     } 
     #mask { 
      display: none !important; 
     } 
     #login{ 
      border:4px solid #008800 !important 
     } 

    </style> 

    <title> 
     Portal Login 
    </title> 
</head> 
<body OnLoad="Start();"> 

    <noscript> 
     <div class="noJavascriptBox"> 
     Your web browser must have JavaScript enabled 
     in order for this application to display correctly. 
     </div> 
    </noscript> 

    <div id="hasJavascript" class="hidden contentContainer"> 
     <div id="container"> 
      <div id="mask"></div> 
      <div id="login"> 
       <h2>Portal Live Login</h2> 
       <form id="login_form" method="POST" onSubmit="return setSubmitUrl(this);"> 
        <label for="j_username">Username:</label> 
        <input type="text" id="j_username" name="j_username" autocapitalize="off" autocorrect="off"/> 
        <label for="j_password">Password:</label> 
        <input type="password" id="j_password" name="j_password" autocapitalize="off" autocorrect="off"/> 
        <label id="failureIndication">&nbsp;</label> 
        <input type="submit" value="OK"/> 
       </form> 
      </div> 
     </div> 
    </div> 
</body> 

答えて

0

私はSSLエラーを無視するサービス引数を渡すことで、問題を解決 - このような(service_args = [ '無視-SSL-エラー=真の']):

driver = webdriver.PhantomJS(desired_capabilities=dcap,service_args=['--ignore-ssl-errors=true']) 

は、その後、それは働きました!

1

私はあなたのためにwaitする必要があると思いますロードするページ、具体的にはパスワードフィールドの表示を待ちます

from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

wait = WebDriverWait(driver, 10) 
password = wait.until(EC.visibility_of_element_located((By.ID, "j_password"))) 
password.send_keys("password") 
+0

残念ながら動作しません。それは単にタイムアウトします。私はステップを50までステップアップしましたが、まだタイムアウトになりました –

+0

@ johansson.lc大丈夫、 'driver.page_source'を印刷するとどうなりますか? – alecxe

+0

確かに、それは私が言及しなかった奇妙な詳細です。見つかったソースはちょうどです。実際のブラウザではなく、私がphantomjsを使用しているからでしょうか?自分のユーザーエージェントを偽装する必要がありますか? –

関連する問題