简体   繁体   English

使用selenium webdriver python使用xpath登录到jsp表单

[英]Logging in to jsp form with xpath using selenium webdriver python

I'm trying to login in to a jsp form with selenium webdriver in python.我正在尝试使用 python 中的 selenium webdriver 登录到 jsp 表单。 I'm trying to login by posting the parameters but I cannot reach the form or anything beyond the body tag for that matter.我正在尝试通过发布参数登录,但我无法访问表单或正文标签之外的任何内容。 What am i doing wrong?我究竟做错了什么? Below is my code and below that is the page source - since it is a non-public web page:下面是我的代码,下面是页面源代码 - 因为它是一个非公开网页:

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()

Page source:页面来源:

<!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>

I think you need towait for the page to load or, to be more specific, wait for the visibility of the password field :我认为您需要等待页面加载,或者更具体地说,等待密码字段的可见性

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

I solved the problem by passing a service argument to ignore ssl errors ( service_args=['--ignore-ssl-errors=true'] ) like this:我通过传递一个服务参数来忽略 ssl 错误( service_args=['--ignore-ssl-errors=true'] )解决了这个问题,如下所示:

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

Then it worked!然后它起作用了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM