【问题标题】:Foreach does not get xpath results from nodeForeach 没有从节点获取 xpath 结果
【发布时间】:2019-11-06 01:33:43
【问题描述】:

我使用xpath webdriver在代码中找到一个div,我需要获取这个div的每个节点上的数据,但这并没有发生。

HTML:

<div class="elements">
    <div class="element"><div class="title">Title A</div></div>
    <div class="element"><div class="title">Title B</div></div>
    <div class="element"><div class="title">Title C</div></div>
</div>

PHP 代码:

$elements = array();
$data = $driver->findElements(WebDriverBy::xpath("//div[@class='elements']//div[@class='element']"));
foreach ($data as $i => $element) {
    $elements[$i]["title"] = $element->findElement(WebDriverBy::xpath("//div[@class='title']"))->getText();
}

返回的结果数组 $elements:

Array
(
    [0] => Array
        (
            [title] => Title A
        )

    [1] => Array
        (
            [title] => Title A
        )

    [2] => Array
        (
            [title] => Title A
        )

)

上面的脚本只返回标题 A 3 次。 我需要它像 xPath [x] 中的数字一样工作。示例:

(//div[@class='elements']//div[@class='element'])[1]//div[@class='title'] 用于标题 A (//div[@class='elements']//div[@class='element'])[2]//div[@class='title'] 标题 B (//div[@class='elements']//div[@class='element'])[3]//div[@class='title'] 标题 C

我不能使用数字,因为 xPath 太大,会弄乱代码。

肯定 foreach 中的正确节点 xPath 不应该工作吗?

【问题讨论】:

    标签: php selenium-webdriver foreach webdriver


    【解决方案1】:

    当使用WebElement 定位另一个WebElementxpath 时,您需要在路径中使用当前上下文.

    $element->findElement(WebDriverBy::xpath(".//div[@class='title']"))
    

    【讨论】:

      猜你喜欢
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 2015-03-30
      • 1970-01-01
      • 2023-03-27
      • 2011-08-26
      相关资源
      最近更新 更多