Trying to avoid detection using selenium chrome driver Java

0 votes

I am trying to do an automated browsing (flight search) but upon trying to submit the form, the website always detects me. I first tried slowing down my form filling procedure using Thread.sleep but that did not work either. 

I also tried the answers suggested in these posts link 1, link 2, link 3, which removed the notification "Chrome is being controlled by automated test software" but I am still detected upon submitting form and asked to verify am human.

Below is my automated browsing code: Fills the form but gets asked to verify on submitting.

System.setProperty("webdriver.chrome.driver", "path\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();                

    //options.addArguments("--headless");                        

    //My avoid detection tries
    options.addArguments("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");                         //specify user agent
    options.setExperimentalOption("excludeSwitches", new String [] {"enable-automation"});
    options.setExperimentalOption("useAutomationExtension", false);
    ChromeDriver driver = new ChromeDriver(options);   //create headless browser
    Map javascriptCodes = Map.of(
            "source", "Object.defineProperty(navigator, 'webdriver', {get: () => undefined }); Object.defineProperty(navigator, 'languages', {get: function() { return ['en-US', 'en']; }, }); Object.defineProperty(navigator, 'plugins', { get: function() { return [1, 2, 3, 4, 5]; }, });"
    );

    driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", javascriptCodes);

    JavascriptExecutor js = driver;

    driver.get("https://www.edureka.co/");

    try {
        //AUTOMATED BROWSING
        //Wait till button is clickable
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        String waitCondition = "bookingModule-submit";
        wait.until(ExpectedConditions.elementToBeClickable(By.id(waitCondition)));

        WaitFor(5);

        //if cookie popup alert, close it
        if (driver.findElements(By.xpath("//button[@class='optoutmulti_button']")).size() != 0) {
            //close popup
            WebElement popUp = driver.findElement(By.xpath("//button[@class='optoutmulti_button']"));
            popUp.click();
            WaitFor(1);
        }

        //Scroll to form
        //Find element by link text and store in variable "Element"
        WebElement Element = driver.findElement(By.id("bookingModule"));
        //This will scroll the page till the element is found
        js.executeScript("arguments[0].scrollIntoView();", Element);

        //Fill in origin
        WebElement origin = driver.findElement(By.xpath("//input[@name='origin']"));
        origin.sendKeys("MIA");
        WaitFor(2);

        //Fill in destination
        WebElement destination = driver.findElement(By.xpath("//input[@name='destination']"));
        destination.sendKeys("LAX");
        WaitFor(2);

        //Clear depart date
        driver.findElement(By.id("aa-leavingOn")).clear();
        WaitFor(1);
        //Fill in depart date
        driver.findElement(By.id("aa-leavingOn")).sendKeys("15/08/2020");

        WaitFor(2);

        //Clear depart date
        driver.findElement(By.id("aa-returningFrom")).clear();
        WaitFor(1);
        //Fill in return date
        driver.findElement(By.id("aa-returningFrom")).sendKeys("30/08/2020");

        WaitFor(2);

        //Submit form
        //class = "btn btn-fullWidth"
        WebElement submitBtn = driver.findElement(By.xpath("//input[@id='bookingModule-submit']"));
        submitBtn.click();
        //UPON CLICKING HERE IS WHERE I GET ASKED TO VERIFY MYSELF :(

        //Set web driver wait to 20 seconds. Waits till it receives the element with id = fare-select-button
        wait = new WebDriverWait(driver, Duration.ofSeconds(20));

        waitCondition = "owd-calendar-slide-previous";
        wait.until(ExpectedConditions.elementToBeClickable(By.className(waitCondition)));

        String innerHTML = driver.findElement(By.className("row upsell-bound")).getAttribute("innerHTML");
        System.out.println(innerHTML);
    }
    catch (Exception ex){
        ex.printStackTrace();
    }

And my sleep method

public static void WaitFor(int seconds) throws InterruptedException {
    seconds = seconds * 1000;
    Thread.sleep(seconds);
}


My chrome browser version is 83. Selenium is 3.141.x, I also tried selenium 4.0.0-alpha-4 to no avail.

Jul 14, 2020 in Java by laiman
• 330 points
4,641 views

Hey, @Iaiman,

Are you facing any kind of error while executing your snippet! If yes, please mention.

No I do not get any error, what I get is a page requiring me to do human verification before my request is processed.
Hey @Laiman, Have you tried using waits?

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java

+1 vote
1 answer

if i given invalid data how to get alert in selenium using java

Hello, Here is some of the method you ...READ MORE

answered Jul 20, 2020 in Java by Niroj
• 82,840 points
1,231 views
0 votes
2 answers

How to read the files using Java?

You can use collections. try (Stream<Path> filePathStream=Files.walk(Paths.get("/home/you/Desktop"))) { ...READ MORE

answered Jul 10, 2018 in Java by Sushmita
• 6,920 points
1,135 views
0 votes
3 answers

Adding text to a file using Java

try { final Path ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,920 points
1,436 views
0 votes
1 answer

How to download and save a file from Internet using Java?

public void saveUrl(final String filename, final String ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
925 views
0 votes
1 answer

How to encode data using Base64 in Java?

import org.apache.commons.codec.binary.Base64; We can not use sun.* packages ...READ MORE

answered May 30, 2018 in Java by Sushmita
• 6,920 points
1,076 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,230 points
2,415 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,767 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,186 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,260 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,898 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP