Xpath查询选中了单选按钮内的父标签文本


xpath query checked radio button parent label text inside

我需要抓取标签内的文本与特定的类中有一个选中的单选输入。

这是HTML:

<div id="ships-from2">
    <label for="ship_hk_intl">
        <input type="radio" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/" value="hk_intl">
            Hong Kong Warehouse - USD44.31
    </label>
    <label for="ship_us_intl">
        <input type="radio" name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked value="us_intl">
            United States Warehouse - USD45.10
    </label>
</div>


我需要:
标签内具有选中单选按钮的字符串。实际的单选按钮可能会改变,所以我需要检查哪一个被选中

我正在抓取dom并使用xpath,但不知道如何编写查询的想法吗?

EDIT 1 - CODE至今(回复@TimDev):

    $div        = $dom->getElementById('ships-from2');
    $query      = '//input[@checked]/../text()';
    $e          = $xpath->query($query, $div);
    echo 'TEST:'.trim($e->item(1)->nodeValue);

您可能需要稍微调整一下查询,但它确实返回单选输入字段,您可以轻松检查所需的属性。

    $html='
        <div id="ships-from2">
            <label for="ship_hk_intl">
                <input type="radio" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/" value="hk_intl">
                    Hong Kong Warehouse - USD44.31
            </label>
            <label for="ship_us_intl">
                <input type="radio" name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked value="us_intl">
                    United States Warehouse - USD45.10
            </label>
        </div>';
        $dom=new DOMDocument;
        $dom->loadHTML( $html );
        $xpath=new DOMXPath( $dom );
        $col=$xpath->query('//label/input');
        foreach( $col as $node ) if( $node->hasAttribute('checked') ) {
            echo $node->getAttribute('value').' '.$node->parentNode->nodeValue;
        }
        $dom=null;
        $xpath=null;

使用xpath可以这样做

//input[@checked]/..

获取文本

//input[@checked]/../text()

function test(field) {
  console.log(field.parentElement.innerText);
}
<div id="ships-from2">
    <label for="ship_hk_intl">
        <input type="radio" onchange="test(this)" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/" value="hk_intl">
            Hong Kong Warehouse - USD44.31
    </label>
    <label for="ship_us_intl">
        <input type="radio" onchange="test(this)"  name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked value="us_intl">
            United States Warehouse - USD45.10
    </label>
</div>

我不知道为什么raghavendra给出了javascript,但这里有一个PHP的例子。他使用//input[@checked]/../text()是对的。

注意: ../text()返回两项文本。它返回所有 input节点周围的文本。这也是<label><input>之间的空白。

这就是为什么在下面的代码片段中,我们得到了第二个带有$e->item(1)->nodeValue

的文本
$html = <<<EOC
<div>
    More HTML!
</div>
<div>
   Even more HTML!
</div>
<div id="ships-from2">
    <label for="ship_hk_intl">
        <input type="radio" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/"
               value="hk_intl"/>
        Hong Kong Warehouse - USD44.31
    </label>
    <label for="ship_us_intl">
        <input type="radio" name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked
               value="us_intl"/>
        United States Warehouse - USD45.10
    </label>
</div>
EOC;
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpathObject = new DOMXPath($doc);
$div = $doc->getElementById('ships-from2');
$query = '//input[@checked]/../text()';
$e = $xpathObject->query($query, $div);
echo trim($e->item(1)->nodeValue);

首先它不是有效的XML。每个属性必须有一个值,所以替换

 ...data-action="http://www.example.com/" checked value="us_intl">

 ata-action="http://www.example.com/" checked="true" value="us_intl" />

那么你的xpath将看起来像这样:

  //input[@checked="true" and  @id="ship_us_intl"]