Таблицы обхода Javascript для получения значений в первом списке - PullRequest
0 голосов
/ 02 мая 2019

Я пытаюсь пройти через эту строку кодов через xpath javascript, но я не могу получить значение первого тега li, который является Thesis [Master's) - University, 2012. ":

<table id="addInformationGraphics" cellpadding="2" cellspacing="0" width="100%">
<TR><td background="/images/icons/general/thickline.gif" nowrap vAlign='top'>
<span class='ColRow'>&nbsp;&nbsp;</span><span class="SectionHeader" ><a name = 'anchorAdditionalInfo'></a>
Additional Info
</span>
</td>
<A name="AdditionalInfo"></A></TR></TABLE>
<table id="addInformation" cellpadding="2" cellspacing="0"><tr>
<td vAlign="top"><IMG SRC="/images/icons/general/spacer.gif" width="20" height="1"></td>
<td class="ColRow"><ul><li>
 Thesis [Master&#39;s) -- University, 2012.</li></ul>     
</td>
</tr>

Вот один из тех, которые я пробую:

  var getaddInformation = document.evaluate("table[contains(@id,'addInformation')]/tbody/tr/td/following- sibling::td/ul/li", document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null );
if (getaddInformation.singleNodeValue) {
var addInformationDetails = getaddInformation.singleNodeValue.textContent;
       }
console.log(addInformationDetails);
    var getaddInformation = document.evaluate("table[contains(@id,'addInformationGraphics')]/following-sibling::table/td/following-sibling::td/ul/li", document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null );
if (getaddInformation.singleNodeValue) {
  var addInformationDetails = getaddInformation.singleNodeValue.textContent;
               }
console.log(addInformationDetails);

И еще один:

var getaddInformation = document.evaluate("table[contains(@id,'addInformationGraphics')]/following-sibling::table/td/following-sibling::td/ul/li", document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null );
if (getaddInformation.singleNodeValue) {
  var addInformationDetails = getaddInformation.singleNodeValue.textContent;
       }
console.log(addInformationDetails);

Для простоты, вот xpath:

"table[contains(@id,'addInformation')]/tbody/tr/td/following-sibling::td/ul/li
And the other one:
 table[contains(@id,'addInformationGraphics')]/following-sibling::table/td/following-sibling::td/ul/li

Что может быть мне не хватает, если я не ошибаюсь, иерархия документа HTML выглядит примерно так:

table 
  tbody 
    tr 
      td
      td
        ul
        li

Заранее спасибо!

1 Ответ

0 голосов
/ 02 мая 2019

При использовании API evaluate вторым аргументом является узел контекста, поэтому, если вы хотите найти HTML table в документе HTML, вы обычно используете document.body с относительным путем, таким как table, чтобы найти table детей из body:

var getaddInformation = document.evaluate("table[contains(@id,'addInformation')]/tbody/tr/td/ul/li", document.body, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null );

if (getaddInformation.singleNodeValue) {
  var addInformationDetails = getaddInformation.singleNodeValue.textContent;
  console.log(addInformationDetails);
}
<table id="addInformationGraphics" cellpadding="2" cellspacing="0" width="100%">
<TR><td background="/images/icons/general/thickline.gif" nowrap vAlign='top'>
<span class='ColRow'>&nbsp;&nbsp;</span><span class="SectionHeader" ><a name = 'anchorAdditionalInfo'></a>
Additional Info
</span>
</td>
<A name="AdditionalInfo"></A></TR></TABLE>
<table id="addInformation" cellpadding="2" cellspacing="0"><tr>
<td vAlign="top"><IMG SRC="/images/icons/general/spacer.gif" width="20" height="1"></td>
<td class="ColRow"><ul><li>
 Thesis [Master&#39;s) -- University, 2012.</li></ul>     
</td>
</tr>
</table>

Или вам нужно изменить XPath, чтобы начать с, например. /html/body/table или //table.

Обратите внимание, однако, что запрос «первого» списка и использование XPathResult.ANY_UNORDERED_NODE_TYPE не даст согласованного результата, если вы ищете что-то на основе порядка документов, тогда используйте FIRST_ORDERED_NODE_TYPE.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...