Попытка избежать обнаружения с помощью драйвера selenium chrome (Java) - PullRequest
1 голос
/ 14 июля 2020

Я пытаюсь выполнить автоматический просмотр (поиск рейсов), но при попытке отправить форму веб-сайт всегда меня обнаруживает. Сначала я попытался замедлить процедуру заполнения формы, используя Thread.sleep, но это тоже не сработало. Я также попробовал ответы, предложенные в этих сообщениях ссылка 1 , ссылка 2 , ссылка 3 , которые удалили уведомление «Chrome контролируется автоматическим тестом. программное обеспечение ", но при отправке формы меня по-прежнему обнаруживают и просят подтвердить, что я человек. Веб-сайт: американский

Ниже приведен мой автоматический код просмотра: заполняет форму, но при отправке его просят подтвердить.

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

И мой метод сна

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

Моя Chrome версия браузера - 83. Selenium - 3.141.x, я также пробовал Selenium 4.0.0-alpha-4, но безрезультатно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...