我有一個診所資料庫,以及每個診所的網址。所有診所頁面在 html/css 方面都是相同的,但要抓取的內容不同。
但是,有些診所的頁面上沒有內容,這給我帶來了麻煩。
我有:
$crawler = $this->client->request('GET', $clinic->url);
$this->client->waitFor('.facility');
如果.facility不存在,waitFor()將throw exception因為timeout. 在這種情況下,我需要能夠繼續,而不是拋出例外。因此,如果超時,它應該繼續而不是結束。
我無法計算設施專案并以這種方式進行檢查,因為這些專案是用 ajax 加載的,并且在頁面加載開始時不存在。
我嘗試和研究的內容:
symfony/panther 是否有可能等待某些元素 n 次?
如何等待 - PHPWebDriver
uj5u.com熱心網友回復:
您可以像這樣捕獲例外...
try
{
$this->client->waitFor('.facility');
}
catch (TimeoutException $e)
{
// Log something here that it was skipped by a timeout...
// PHP will continue
}
在您的類的頂部,您可能需要添加(這就是代碼看起來正在使用的內容。):
use Facebook\WebDriver\Exception\TimeoutException;
另請注意,該函式還有其他可能有用的引數:
/**
* @param string $locator The path to an element to be waited for. Can be a CSS selector or Xpath expression.
*
* @throws NoSuchElementException
* @throws TimeoutException
*/
public function waitFor(string $locator, int $timeoutInSecond = 30, int $intervalInMillisecond = 250): PantherCrawler
{
....
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/475559.html
