在块中查找元素

时间:2017-01-02 11:42:13

标签: selenium xpath

我正在尝试获取状态字段,如屏幕截图Attached Image

所示
<tbody>
    <tr>
        <td>STATUS</td>
        <td>TXN_SUCCESS</td>
    </tr>
<tbody>

如何在selenium中找到此元素。一种方法是使用Xpath,但xpath动态变化,因此下次我试图找到这个元素时它会失败?

我找到的Xpath是/html/body/div[3]/center[2]/table/tbody/tr[10]/td[1]

任何人都可以帮助我吗?完整的HTML如下

</style>
</head>
<body style="margin-top: 10px">
    <div id="logo" style="float:left; margin-left: 20px">
        <a href="http://www.test.com" target="_blank">
            <img src="images/test.png" alt="" />
        </a>
    </div>
    <div id="logo" style="float:right; margin-right: 20px">
        <a href="http://www.test.com" target="_blank">
            <img src="images/test_logo.png" alt="" />
        </a>
    </div>
    <div style="float: left; width: 100%;">
        <center><h3>Response</h3></center>
        <p1> <font color="red">mid key not in property </font></p1>
        <center>
            <table border="1px; text-align: center;" class="tablesorter1"">
            <thead>
            <tr>
                <th>Parameter Name</th>
                <th>Parameter Value</th>
            </tr>
            </thead>

                    <tr>
                        <td>BANKNAME</td>
                        <td>HDFC</td>
                    </tr>


                    <tr>
                        <td>BANKTXNID</td>
                        <td>3816913151570020</td>
                    </tr>


                    <tr>
                        <td>CURRENCY</td>
                        <td>INR</td>
                    </tr>


                    <tr>
                        <td>GATEWAYNAME</td>
                        <td>HDFC</td>
                    </tr>


                    <tr>
                        <td>MID</td>
                        <td>hybref63266728877901</td>
                    </tr>


                    <tr>
                        <td>ORDERID</td>
                        <td>8956552</td>
                    </tr>


                    <tr>
                        <td>PAYMENTMODE</td>
                        <td>CC</td>
                    </tr>


                    <tr>
                        <td>RESPCODE</td>
                        <td>01</td>
                    </tr>


                    <tr>
                        <td>RESPMSG</td>
                        <td>Txn Success</td>
                    </tr>


                    <tr>
                        <td>STATUS</td>
                        <td>TXN_SUCCESS</td>
                    </tr>


                    <tr>
                        <td>TXNAMOUNT</td>
                        <td>1.00</td>
                    </tr>


                    <tr>
                        <td>TXNDATE</td>
                        <td>2017-01-02 15:15:05.0</td>
                    </tr>


                    <tr>
                        <td>TXNID</td>
                        <td>20170102111212800110166787500006704</td>
                    </tr>


                <tr>
                <td>Valid Checksum</td>
                <td>false</td>
                </tr>
            </table>
        </center>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您可以使用以下xpath来提取状态 -

//tr/td[contains(text(),'STATUS')]/following-sibling::td 

您可以获得如下代码(Java代码示例)的状态,并可根据您的要求进行操作 -

    String status = driver.findElement(By.xpath("//tr/td[contains(text(),'STATUS')]/following-sibling::td")).getText();
    if(status.equalsIgnoreCase("TXN_SUCCESS"))
    {
        System.out.println("Transaction Success");
    }
    else
    {
        System.out.println("Transaction Failed");
    }

答案 1 :(得分:0)

您可以轻松使用find来命名td

//td[text()='STATUS']

查找值,使用:

//td[text()='STATUS']/../td[2]