Search code examples
c#seleniumselenium-webdriverwebdriverrecaptcha

How to Find the reCAPTCHA element and click on it in c# Selenium


Hello everyone I need some help. There is URL: http://lisans.epdk.org.tr/epvys-web/faces/pages/lisans/petrolBayilik/petrolBayilikOzetSorgula.xhtml. As you can see in screenshot I need to click checkbox Captcha.

https://i.sstatic.net/xjXaA.png

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace AkaryakitSelenium
{
    class Program
    {
        private static string AkaryakitLink = "http://lisans.epdk.org.tr/epvys-web/faces/pages/lisans/petrolBayilik/petrolBayilikOzetSorgula.xhtml";
        static void Main(string[] args)
        {
           
           IWebDriver driver = new ChromeDriver();
            IJavaScriptExecutor js = driver as IJavaScriptExecutor;
            driver.Navigate().GoToUrl(AkaryakitLink);
            var kategoriCol = driver.FindElements(By.CssSelector(".ui-selectonemenu-trigger.ui-state-default.ui-corner-right"));
            var x = kategoriCol[3];
            x.Click();
            var deneme = driver.FindElement(By.Id("petrolBayilikOzetSorguKriterleriForm:j_idt52_1"));
             deneme.Click();
           
            
            var check = driver.FindElement(By.Id("recaptcha-anchor"));
            
            check.Click();
        }
    }
}

And lastly this error that I am facing:

"OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"#recaptcha-anchor"}"

Thank you for your help.


Solution

  • The element you are looking for is inside an iframe :

    //iframe[@title='reCAPTCHA']
    

    first you need to switch to iframe like this :

    new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.XPath("//iframe[@title='reCAPTCHA']")));
    

    then you can perform a click on it :

    var check = driver.FindElement(By.Id("recaptcha-anchor"));
    check.Click();
    

    PS : captchas are not meant to be automated. since Captcha stands for CAPTCHA stands for the Completely Automated Public Turing test to tell Computers and Humans Apart.