0

I have this code with php-webdriver library in Selenium to click hCAPTCHA's checkbox square button but results to an error instead. See code below...

$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();

But results to an error below...

Fatal error: Uncaught Facebook\WebDriver\Exception\NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"/html/body/div/div[1]/div[1]/div"} 
rene
  • 41,474
  • 78
  • 114
  • 152
gprialde
  • 1
  • 4

1 Answers1

0

In the step $driver->findElement(WebDriverBy::id('/html/body/div/div[1]/div[1]/div')); we are specifying to search element by id but we are passing the absoulte xpath for the element

You need to pass the id of the captcha to click on it

Your solution would look like

$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();
Darshan Shah
  • 246
  • 1
  • 4
  • I tried the code suggested above but still got this error, "**Fatal error: Uncaught Facebook\WebDriver\Exception\NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"checkbox"} (Session info: chrome=103.0.5060.66) (Driver info: chromedriver=103.0.5060.53 (a1711811edd74ff1cf2150f36ffa3b0dae40b17f-refs/branch-heads/5060@{#853}),platform=Windows NT 10.0.20348 x86_64) (WARNING: The server did not provide any stacktrace information)**". – gprialde Jul 02 '22 at 09:18
  • Hi @gprialde updated the solution the captcha element sometimes takes time to load on the page so we need to check for presence of element using webdriver wait . Can you retry – Darshan Shah Jul 02 '22 at 11:31
  • Hi @gprialde can you check the above update solution – Darshan Shah Jul 03 '22 at 05:21
  • Yes I have done it and it works. Thanks! – gprialde Jul 05 '22 at 20:21