Selenium+PHPunit:foreach 集合中的元素

发布于 2024-10-31 21:57:22 字数 405 浏览 8 评论 0 原文

我正在寻找一种使用 PHPunit 处理 Selenium 中的元素集合的方法。假设我有以下代码:

<div class="test">aaa</div>
<div class="test">bbb</div>
<div class="test">ccc</div>

我希望能够处理每个循环内的所有

元素,假设通过使用 根据其类选择元素//div[@class='test']

$divs = ...  // 
foreach ($divs as $div) {
 // do something
}

I am looking for a way to work with collections of elements in Selenium with PHPunit. Let's say I have the below code:

<div class="test">aaa</div>
<div class="test">bbb</div>
<div class="test">ccc</div>

I would like to be able to work on all the <div> elements inside an each loop, let's say by selecting the elements based on their class with //div[@class='test']

$divs = ...  // 
foreach ($divs as $div) {
 // do something
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平安喜乐 2024-11-07 21:57:22

获取整个页面内容的函数是:- getHtmlSource()
因此,加载 HTML 的最终函数调用将类似于 ..
$dom->loadHTML($this->getHtmlSource());

The function to get the content of your entire page is :- getHtmlSource()
So your final function call to load HTML would be something like ..
$dom->loadHTML($this->getHtmlSource());

素罗衫 2024-11-07 21:57:22

在 PHP 中,如果您想处理一些 HTML 数据,一个很好的解决方案是使用 DOMDocument-- 这意味着能够通过其 //www.php.net/manual/en/domdocument.loadhtml.php" rel="nofollow noreferrer">DOMDocument::loadHTML() 方法。

Loading your HTML with DOMDocument :

$dom = new DOMDocument();
$dom->loadHTML('HERE YOUR HTML STRING');

然后,您可以实例化 DOMXpath对象:

$xpath = new DOMXPath($dom);

Which will allow you to extract data from the HTML, using XPath Queries, with [**`DOMXPath::query()`**][4] :

$entries = $xpath->query('//div[@class="test"]'); // notice quotes
if ($entries->length > 0) {
    foreach ($entries as $entry) {
        // Work on the $entry -- which is a DOM node/element
    }
}

In PHP, if you want to work with some HTML data, a great solution is using the DOMDocument class -- which means being able to work with DOM methods -- via its DOMDocument::loadHTML() method.

Loading your HTML with DOMDocument :

$dom = new DOMDocument();
$dom->loadHTML('HERE YOUR HTML STRING');

You can then instanciate a DOMXpath object :

$xpath = new DOMXPath($dom);

Which will allow you to extract data from the HTML, using XPath Queries, with [**`DOMXPath::query()`**][4] :

$entries = $xpath->query('//div[@class="test"]'); // notice quotes
if ($entries->length > 0) {
    foreach ($entries as $entry) {
        // Work on the $entry -- which is a DOM node/element
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文