有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Selenium/Java中动态表中第n个td的xpath

我有一个动态表,需要获取“日期/时间”列下的所有值。我无法获取包含日期/时间的/td。td ID和类名都是动态的

<document>
 <html xmlns="http://www.w3.org/1999/xhtml">
  <head id="Head1">
  <body>
   <form id="form1" action="RunReport.aspx?ReportPath=%2fxxxxxx%2fChange+Log&ReportName=Change+Log&ReportOutput=--" method="post" name="form1">
    <div/>
    <script type="text/javascript"> //<![CDATA[ var theForm = document.forms['form1']; if (!theForm) {     theForm = document.form1; } function __doPostBack(eventTarget, eventArgument) {     if (!theForm.onsubmit || (theForm.onsubmit() != false)) {         theForm.__EVENTTARGET.value = eventTarget;         theForm.__EVENTARGUMENT.value = eventArgument;         theForm.submit();     } } //]]> </script>
    <script type="text/javascript" src="/xxxxxxxx/WebResource.axd?d=V7QB9aA-CMKBf_It9wv5y2DqZbRLrb5ffBwJnLkaYDr5pU-NN0s1ZPyMlic7TalUdeqQfQg0f0hBQ5vc5FRF6TGqLTthE4DKA30A-L1LPCg1&t=635418606520000000"/>
   <script type="text/javascript" src="/xxxxxxxx/ScriptResource.axd?d=ZQO5N5aOeLdEjEooaJaTyKww9aony6pg5nLqvVGBchaBRFjQEDHDikotXjab4lpgUsn8rNEgGqrrEmvFvTEJh9XNGP0OvUXTFQk6ZnGiqxCgD6yxZEIZNQeIUFyY7s0UJiH9S_XEit5ruUgYEwmnGToVkDm1twhe03OvidFuqGs1&t=7e632e9f"/>
    <script type="text/javascript" src="/xxxxxxxxx/ScriptResource.axd?d=TraUIU0pF-vc5erOeo272dRc6jvvl-tp1IhH8icmW9Pqh-XyqQGubtk3taZPhhwGJyBEJq2uA1HlRhC55tsZ3PBPoT81bFotHP5mZF_KuY_2NJVoyMZznUTOBX5aqbBYnW9p4FEn3lnTyUpAjLCwlpk17z9yYmrw8ErPm6dTXg0LDVW1Jt40I6C1yk-hY_j70&t=7e632e9f"/>
   <script type="text/javascript" src="/xxxxxxxx/Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=10.0.30319.1&Name=ViewerScript"/>
    <script type="text/javascript" src="/xxxxxxxx/ScriptResource.axd?d=ZgCktqsR0I_2fhh-iIO4Abgy21lP5uMQNwtUzd_UyFfIprbIySdOXIPYBkpeZLT76IM5l0FiTvM__QYNu9JorZxtU13SyIYAffijp3IaAfJimLPcVFjaytrZe-noWDW4P0ZdWdc7kR8Vn5mci2VdV2Lj86BByElGhI9fC7p5o8k1&t=7e632e9f"/>
    <div/>
    <div/>
    <script type="text/javascript"> //<![CDATA[ Sys.WebForms.PageRequestManager._initialize('Scriptmanager1', 'form1', ['fReportViewer1$ReportViewer','','fReportViewer1$Toolbar','','fReportViewer1$DocMap','','fReportViewer1$ctl10$ReportArea',''], [], ['ReportViewer1$ctl10$ReportControl$ctl00',''], 90, ''); //]]> </script>
    <noscript> &nbsp;Your browser does not support scripts or has been configured not to allow scripts. </noscript>
<span id="ReportViewer1_ReportViewer">
  <div id="ReportViewer1" style="height: 100%; width: 100%;" onactivate="if ($get('ReportViewer1_ctl04') != null && $get('ReportViewer1_ctl04').control != null) $get('ReportViewer1_ctl04').control.HideActiveDropDown();" onclick="if ($get('ReportViewer1_ctl04') != null && $get('ReportViewer1_ctl04').control != null) $get('ReportViewer1_ctl04').control.HideActiveDropDown();">
      <div id="ReportViewer1_HttpHandlerMissingErrorMessage" style="border-color:Red;border-width:2px;border-style:Solid;padding:10px;display:none;overflow:auto;font-size:.85em;">
      <span id="ReportViewer1_ctl03">
      <input id="ReportViewer1_ctl11" type="hidden" name="ReportViewer1$ctl11" value="ltr"/>
      <input id="ReportViewer1_ctl12" type="hidden" name="ReportViewer1$ctl12" value="standards"/>
      <div id="ReportViewer1_AsyncWait" style="background-color: White; opacity: 0.7; position: absolute; display: none; top: 29px; left: 0px; width: 1049px; height: 0px; z-index: 1000; visibility: hidden;"/>
      <div id="ReportViewer1_AsyncWait_Wait" style="cursor: wait; background-color: rgb(236, 233, 216); padding: 15px; border: 1px solid black; display: none; position: absolute; z-index: 1001; visibility: hidden; top: 0px; left: 441.5px;">
      <input id="ReportViewer1_AsyncWait_HiddenCancelField" type="hidden" value="False" name="ReportViewer1$AsyncWait$HiddenCancelField"/>

下表id与上一个输入id处于同一级别

<table id="ReportViewer1_fixedTable" cellspacing="0" cellpadding="0" style="table-layout:fixed;width:100%;height:100%;">
<tbody>
<tr style="background-color:#ECE9D8;">
<tr id="ParametersRowReportViewer1" style="display:none;">
<tr style="height:6px;font-size:2pt;display:none;">
<tr>
<tr>
  <td style="vertical-align:top;width:25%;height:100%;display:none;">
  <td style="display:none;width:4px;padding:0px;margin:0px;height:100%;vertical-align:middle;background-color:#ECE9D8;">
   <td style="height:100%;vertical-align:top;">
  <div id="ReportViewer1_ctl10" style="height:100%;width:100%;overflow:auto;position:relative;">
  <div id="VisibleReportContentReportViewer1_ctl10" style="height: 100%;">
  <div id="P7d543a84a5b9486884b84551ef9c3d9d_1_oReportDiv" style="WIDTH:100%;direction:ltr" dir="LTR">
  <table cellspacing="0" cellpadding="0">
  <tbody>
   <tr>
    <td id="P7d543a84a5b9486884b84551ef9c3d9d_1_oReportCell">
     <table cellspacing="0" cellpadding="0">
      <tbody>
       <tr>
      <tr>
       <td>
        <table cellspacing="0" cellpadding="0" border="0">
         <tbody>
          <tr>
           <td style="vertical-align:top">
             <table cellspacing="0" cellpadding="0" border="0" style="WIDTH:176.06mm;min-width:176.06mm;HEIGHT:241.30mm;">
             <tbody>
              <tr>
               <td style="vertical-align:top">
                <table class="Ada2aa9230a6045f4858a262591be2439124xB" cellspacing="0" cellpadding="0" border="0" style="WIDTH:176.06mm;min-width:176.06mm;HEIGHT:241.30mm;">
                 <tbody>
                  <tr>
                   <td style="vertical-align:top">
                    <table class="Ada2aa9230a6045f4858a262591be2439122" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;" cols="10">
                     <tbody>
                      <tr height="0">
                       <tr valign="top">
                        <td style="HEIGHT:6.35mm"/>
                          <td class="Ada2aa9230a6045f4858a262591be243941c">
                             <td class="Ada2aa9230a6045f4858a262591be243945c">
                                <div class="Ada2aa9230a6045f4858a262591be243945">Date/Time</div>
                                   </td>

我在网上搜索并尝试了这个xpath,但没有成功。谢谢你的帮助

List<WebElement> getDates = driver
    .findElements(By
    .xpath("//table[@id='ReportViewer1_fixedTable']//tr[contains(td[11], 'Date/Time')]"));

    for (int i = 0; i < getDates.size(); i++) {
        String strDateFrmRslt = getDates.get(i).getText();

    }

我似乎不能发布这个问题,因为我需要添加更多细节,所以我在这里再次重复我的问题 我有一个动态表,需要获取“日期/时间”列下的所有值。我无法获取包含日期/时间的/td。td ID和类名都是动态的


解决方案: 如果有人感兴趣。多亏了Ian Robert关于xpath的文章,我才能够使用//table[@id='ReportViewer1\u fixedTable']//tr/td[3]从表中获取所有文本 然后我从那里得到了日期时间值


共 (1) 个答案

  1. # 1 楼答案

    试试这个xpath

    //table//td/div[contains(text(), 'Date/Time')]/..
    

    此xpath查找包含文本“Date/Time”的tddiv,然后返回一级到td,而不需要了解任何有关ID或类的信息

    您尝试的xpath的问题是(1)它依赖于表中的id,(2)它查找的是tr,而不是td