原创

Webドライバーのチェックに合格する可能性はありますか?

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I am trying to automate to get the new messages from web site. I am using Selenium webdriver. I tried to use different browsers and different ways to pass the web driver checks on a web driver checks. but al the time I receive the message :

> org.openqa.selenium.TimeoutException: timeout: Timed out receiving message from renderer: 9.657
  (Session info: MicrosoftEdge=123.0.2420.81)
Build info: version: '4.18.1', revision: 'b1d3319b48'
System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '22'
Driver info: org.openqa.selenium.edge.EdgeDriver
Command: [8cdc6e8cd7c8713b9cd16025a46db807, get {url=https://announcements.md/}]
Capabilities {acceptInsecureCerts: false, browserName: MicrosoftEdge, browserVersion: 123.0.2420.81, fedcm:accounts: true, ms:edgeOptions: {debuggerAddress: localhost:60657}, msedge: {msedgedriverVersion: 123.0.2420.81 (8acdca46cf48..., userDataDir: C:\Users\ARTIOM~1.ORI\AppDa...}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:60657/devtoo..., se:cdpVersion: 123.0.2420.81, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: 8cdc6e8cd7c8713b9cd16025a46db807

at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
at org.openqa.selenium.remote.ErrorCodec.decode(ErrorCodec.java:167)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:138)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:50)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:190)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:216)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:174)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:519)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:301)
at Pages.HomePage.HomePage.<init>(HomePage.java:15)
at Tests.TestAuth.testAuth(TestAuth.java:11)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)

I was trying to use some additional scripts, which helps to make browser to not check that webdriver is used, but any way web site detects it. For example :

  1. Modify WebDriver Properties: WebDriver often injects properties into the browsing environment, such as navigator.webdriver. You can attempt to modify or delete these properties to avoid detection. For example:

    Object.defineProperty(navigator, 'webdriver', {
        get: () => false,
    });
    
  2. Override WebDriver Functions: Some websites may check for WebDriver-specific functions like navigator.webdriver, window.chrome, or document.hasFocus(). You can try overriding these

    functions to return values that mimic a regular browser:
    window.navigator.chrome = {
        runtime: {},
    };
    
  3. Modify User-Agent: WebDriver instances often have unique User-Agent strings. You can try changing the User-Agent to mimic that of a regular browser:

    Object.defineProperty(navigator, 'userAgent', {
        get: () => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 
    Safari/537.36',
    });
    
  4. Modify Function Prototypes: Some WebDriver checks may rely on modifying function prototypes. You can attempt to revert these modifications:

    const isPrototypeMethod = (method) => { return Object.prototype.hasOwnProperty.call(method, 'prototype'); };

    const modifyPrototype = (object, method, modification) => {
        const original = object[method];
        object[method] = modification(original);
    };
    
    // Example: Revert modifications to the `toString` method of `Error.prototype`
    modifyPrototype(Error.prototype, 'toString', (original) => {
        return function() {
            return original.apply(this, arguments);
        };
    });
    

I'm most confused by the line : Session ID: 8cdc6e8cd7c8713b9cd16025a46db807 Can it be the reason that I can not pass the checking? Can it be that the server generate the unique session id for every browser and no possible to use webdriver on this site?

My code is :

abstract public class BaseSeleniumTest {

protected WebDriver driver;

@Before
public void setup() {

    WebDriverManager.edgedriver().setup();
    driver = new EdgeDriver();
    JavascriptExecutor js = (JavascriptExecutor) driver;
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    js.executeScript(" Object.defineProperty(navigator, 'webdriver', {get: () => false,});");
    js.executeScript("");
    js.executeScript("Object.defineProperty(navigator, 'userAgent', {get: () => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',}); ");
    BaseSeleniumPage.setWebDriver(driver);
}

@After
public void finish() {
    try {
        Thread.sleep(3000);
        driver.quit();
    } catch (InterruptedException ex) {
        System.out.println(ex.getMessage());
    }
}

}

and the Main test :

public class TestAuth extends BaseSeleniumTest {

@Test
public void testAuth() throws InterruptedException{
    HomePage homePage = new HomePage();
    homePage.authenticate().authPage();
    Thread.sleep(5000);
}

}
正文到此结束
热门推荐
本文目录