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. The website is american

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.americanairlines.ie/intl/ie/index.jsp");

    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 in Java by laiman
• 330 points
147 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

0 votes
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 in Java by Niroj
• 44,800 points
49 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,890 points
155 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,890 points
120 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 24, 2018 in Java by Rishabh
• 3,580 points
112 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,890 points
156 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
• 55,620 points
643 views
0 votes
1 answer

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,510 points
3,158 views
0 votes
1 answer
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,890 points
2,372 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,890 points
2,524 views