Modifying the Javascript Navigator object with Selenium

前端 未结 1 1110
甜味超标
甜味超标 2021-01-13 16:46

I tried accessing a site with Selenium (with geckodriver) and it said I was blocked but I can access it manually with the Firefox Browser. So I compared the components of my

1条回答
  •  [愿得一人]
    2021-01-13 17:45

    Refer to this issue: Selenium webdriver: firefox headless inject javascript to modify browser property

    it provides a useful pathway.

    This is the code:

    import os
    from selenium import webdriver
    options=webdriver.FirefoxOptions()
    options.set_headless(True)
    driver=webdriver.Firefox(options=options)
    # solution found here https://stackoverflow.com/questions/17385779/how-do-i-load-a-javascript-file-into-the-dom-using-selenium
    driver.execute_script("var s=window.document.createElement('script'); s.src='javascriptFirefox.js';window.document.head.appendChild(s);")
    driver.get('https://auth.citromail.hu/regisztracio/')
    

    Javascript file javascriptFirefox.js

    // overwrite the `languages` property to use a custom getter
    const setProperty = () => {
        Object.defineProperty(navigator, "languages", {
            get: function() {
                return ["en-US", "en", "es"];
            }
        });
    
        // Overwrite the `plugins` property to use a custom getter.
        Object.defineProperty(navigator, 'plugins', {
            get: () => [1, 2, 3, 4, 5],
        });
    
        // Pass the Webdriver test
        Object.defineProperty(navigator, 'webdriver', {
          get: () => false,
        });
        callback();
    };
    setProperty();
    

    0 讨论(0)
提交回复
热议问题