4

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 fingerpirnt and the only difference was that in the Navigator object "webdriver" was set to "true" when I used Selenium. I tried running this code:

from selenium import webdriver

from selenium.webdriver.firefox.options import Options
firefox_binary = '/usr/bin/firefox'
options = Options()
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().FIREFOX
# caps["pageLoadStrategy"] = "normal"  #  complete
caps["pageLoadStrategy"] = "eager"  #  interactive
injected_javascript=("Object.defineProperty(navigator, 'webdriver', { value: 'false' })")
driver = webdriver.Firefox(executable_path=r'/home/kkkk/ggecko/geckodriver', firefox_binary=firefox_binary)
driver.get('https://auth.citromail.hu/regisztracio/')

driver.execute_async_script(injected_javascript)

but it just loaded page with "webdriver" still set to "true", then returned this message:

Traceback (most recent call last):
  File "/home/kkkk/driverr.py", line 14, in <module>
    driver.execute_async_script(injected_javascript)
  File "/home/kkkk/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 652, in execute_async_script
    'args': converted_args})['value']
  File "/home/kkkk/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
    self.error_handler.check_response(response)
  File "/home/kkkk/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Timed out

What am I doing wrong or is there a different way of accomplishing this?

1

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

it won't work after the page is loaded, have you even tried what you wrote?
Another potential issue, you want to have the webdriver = undefined, false is a giveaway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.