我在Selenium中使用php-webdriver库来单击hCAPTCHA的复选框方形按钮,但结果却是错误。
$driver = RemoteWebDriver::create($host, $capabilities);
$driver->get('https://accounts.hcaptcha.com/demo?sitekey=f5561ba9-8f1e-40ca-9b5b-a0b3f719ef34');
print ($driver->getTitle());
$iframe = $driver->findElement(WebDriverBy::xpath("/html/body/div[5]/form/fieldset/ul/li[2]/div/div/iframe"));
$driver->switchTo()->frame($iframe);
$checkbox = $driver->findElement(WebDriverBy::id('/html/body/div/div[1]/div[1]/div'));
$checkbox->click();
但是结果是下面的错误…
Fatal error: Uncaught FacebookWebDriverExceptionNoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"/html/body/div/div[1]/div[1]/div"}
在步骤$driver->findElement(WebDriverBy::id('/html/body/div/div[1]/div[1]/div'));
中,我们指定按id搜索元素,但我们传递的是元素
absoulte xpath
你需要通过验证码的id
来点击它
你的解决方案看起来像
$driver = RemoteWebDriver::create($host, $capabilities);
$driver->get('https://accounts.hcaptcha.com/demo?sitekey=f5561ba9-8f1e-40ca-9b5b-a0b3f719ef34');
print ($driver->getTitle());
$iframe = $driver->findElement(WebDriverBy::xpath("/html/body/div[5]/form/fieldset/ul/li[2]/div/div/iframe"));
$driver->switchTo()->frame($iframe);
$driver->wait(5, 500)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::cssSelector('#checkbox')));
$checkbox = $driver->findElement(WebDriverBy::cssSelector('#checkbox'));
$checkbox->click();