我只想对下面的HTML数据运行一些断言。我在$this->assertEquals(1, ..., "failure");
内尝试过:
$crawler->filter('table.table-general > tr > td:contains("<strong>Task ID</strong> 6")')
$crawler->filter('table.table-general > tr > td:contains("<strong>Customer</strong> ABC Inc.")')
$crawler->filter('table.table-general > tr > td:contains("<strong>Location</strong> New York City")')
$crawler->filter('table.table-general > tr > td:contains("<strong>Phone Number</strong> 555-1234")')
但不幸的是它不起作用(断言失败)。我做错了什么?
<table class="table table-curved table-general">
<tr>
<td rowspan="5" width="10%" id="general-task-id"><strong>Task ID</strong><br /><h3>6</h3></td>
<td width="30%"><strong>Customer</strong> ABC Inc.</td>
<td width="30%"><strong>Location</strong> New York City</td>
<td width="30%"><strong>Phone Number</strong> 555-1234</td>
</tr>
...
答案 0 :(得分:1)
filter方法使用CssSelector并且contains包含基于节点的xpath选择,因此无法检查其中的其他html标记。 这是一个适用于您的示例的可行解决方案:
$i = $crawler->filter('table.table-general > tr > td > strong:contains("Task ID")')->count();
$this->assertEquals(1, $i);
$i = $crawler->filter('table.table-general > tr > td > h3:contains("6")')->count();
$this->assertEquals(1, $i);
$i = $crawler->filter('table.table-general > tr > td > strong:contains("Customer")')->count();
$this->assertEquals(1, $i);
$i = $crawler->filter('table.table-general > tr > td:contains("ABC Inc.")')->count();
$this->assertEquals(1, $i);
希望这个帮助
答案 1 :(得分:0)
您的第一个断言会查找<strong>Task ID</strong> 6
,但实际上您的HTML代表<strong>Task ID</strong><br /><h3>6</h3>