XPATH 选择嵌套元素后选择一个 div?

问题描述 投票:0回答:1

我想制作一个通用标识符以在标签后找到下拉菜单。我有 10 个不同的表格,所有表格都具有相同的格式,但具有不同的 ID、名称等,所以我不能使用它们。相反,我正在寻找下拉列表上方的标签,然后选择包含下拉列表的下一个 div。

想法是在找到具有“标题”的标签后选择下拉列表

我尝试了多种不同的 XPath。尝试这样的事情:

// select[@class='form-control cat_item_option ']/following-sibling::span[text()='Title']

编辑**代码在这里:

<div class="form-group is-required sc-row sc-row-12"><div id="label_IO:d027ec571b332450c82599361a4bcbb2" class="" nowrap="true" type="18" oncontextmenu="return contextQuestionLabel(event, 'd027ec571b332450c82599361a4bcbb2', '18');"><label for="IO:d027ec571b332450c82599361a4bcbb2" class="col-xs-12 control-label"><span id="status.IO:d027ec571b332450c82599361a4bcbb2" title="Mandatory - must be populated before Submit" mandatory="true" oclass="" class=" required-marker" data-original-title="Mandatory - must be populated before Submit">&nbsp;</span><img src="images/s.gifx" style="width:1px; margin-right:0px; margin-left:0px" id="section508.IO:d027ec571b332450c82599361a4bcbb2" alt="Mandatory - must be populated before Submit" title="Mandatory - must be populated before Submit" data-original-title="Mandatory - must be populated before Submit"><span class="sn-tooltip-basic " data-toggle="tooltip" data-placement="auto" title="Title" data-original-title="Title">Title</span></label></div><div class="col-xs-12 form-field input_controls "><select name="IO:d027ec571b332450c82599361a4bcbb2" id="IO:d027ec571b332450c82599361a4bcbb2" class="form-control cat_item_option " onchange="if (typeof(variableOnChange) == 'function') variableOnChange('IO:d027ec571b332450c82599361a4bcbb2')"><option value="" selected="SELECTED" role="option">-- None --</option><option value="b30330019f22120047a2d126c42e70ff" role="option">Chief Executive Officer</option><option value="f30330019f22120047a2d126c42e70ff" role="option">Chief Financial Officer</option><option value="370330019f22120047a2d126c42e70ff" role="option">Chief Technology Officer</option><option value="770330019f22120047a2d126c42e70ff" role="option">Director of Application Development</option><option value="b70330019f22120047a2d126c42e70ff" role="option">Director of Infrastructure</option><option value="f70330019f22120047a2d126c42e70ff" role="option">Director of Procurement</option><option value="3b0330019f22120047a2d126c42e70ff" role="option">Director of Security</option><option value="7b0330019f22120047a2d126c42e70ff" role="option">Director, Audit &amp; Risk</option><option value="bb0330019f22120047a2d126c42e70ff" role="option">Director, Finance Strategy</option><option value="fb0330019f22120047a2d126c42e70ff" role="option">HR Administrator</option><option value="3f0330019f22120047a2d126c42e70ff" role="option">HR Manager</option><option value="7f0330019f22120047a2d126c42e70ff" role="option">Infrastructure Analyst</option><option value="bf0330019f22120047a2d126c42e70ff" role="option">Procurement Administrator</option><option value="ff0330019f22120047a2d126c42e70ff" role="option">Recruitment Administrator</option><option value="330370019f22120047a2d126c42e7000" role="option">Recruitment Manager</option><option value="730370019f22120047a2d126c42e7000" role="option">Security Analyst</option><option value="b30370019f22120047a2d126c42e7000" role="option">Senior Auditor</option><option value="f30370019f22120047a2d126c42e7000" role="option">Senior Developer</option><option value="370370019f22120047a2d126c42e7000" role="option">Senior Finance Analyst</option><option value="770370019f22120047a2d126c42e7000" role="option">SVP, Human Resources</option></select><input type="HIDDEN" class="cat_item_option" name="sys_original.IO:d027ec571b332450c82599361a4bcbb2" id="sys_original.IO:d027ec571b332450c82599361a4bcbb2" value=""></div></div>
javascript html css selenium-webdriver css-selectors
1个回答
0
投票

看起来 XPath 类似于

"../../div[2]/select"
,使用this answer中的函数你可以找到你的选择。

话虽这么说,标签应该有一个

for
应该能够利用的属性

const origin = document.querySelector("label");

const targetByWalk = origin.parentElement.parentElement.querySelector("select");
console.log("targetByWalk", targetByWalk);

const targetByForProperty = document.getElementById(origin.getAttribute("for"));
console.log("targetByForProperty", targetByForProperty);

// https://stackoverflow.com/a/14284815/17519505
function getElementByXpath(path, origin = document) {
  return document.evaluate(
    path,
    origin,
    null,
    XPathResult.FIRST_ORDERED_NODE_TYPE,
    null
  ).singleNodeValue;
}
const targetByXPath = getElementByXpath("../../div[2]/select", origin);
console.log("targetByXPath", targetByXPath);
<div>
  <div>
    <label for="some-id">Choose a fruit</label>
  </div>
  <div>
    <select id="some-id">
      <option>apple</option>
      <option>banana</option>
    </select>
    <p id="a-similar-id"><i>blah blah</i></p>
  </div>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.