webfp / tor-browser-selenium

Tor Browser automation with Selenium.
MIT License
528 stars 101 forks source link

Cannot install extensions. #188

Closed SheldonPatnett closed 1 year ago

SheldonPatnett commented 1 year ago

How can you use extensions? It's not quite clear in tbdriver.py. I'm passing in a list of extensions ie extensions = [] extensions.append('/DEU/workers/Browser/adblock_plus-3.17.xpi')

Then initializing the driver:

    self.driver = TorBrowserDriver(
        tbb_path=f'{BASE_DIR}',
        extensions=extensions,
        pref_dict=pref_dict,
        options=options
    )
gunesacar commented 1 year ago

Your code looks right. See also the test code in test_should_install_custom_extension for reference: https://github.com/webfp/tor-browser-selenium/blob/462b027961afc3757a935072495fd467af2930de/tbselenium/test/test_addons.py#L48-L49

If the problem persists, please check whether the options or pref_dict is interfering, and share the error message you get, if any.

SheldonPatnett commented 1 year ago

Thank you for the speedy reply! The TBB_PATH used to launch the browser doesn't work, it says it needs proper path or binarary.. In the test. In production I use 'workers' folder which has a subfolder of Browser which the current TOR browser bundle.

SheldonPatnett commented 1 year ago

Sorry for not including my current unit test, please find it below

`import unittest from os.path import dirname, realpath, join from tbselenium.test.fixtures import TBDriverFixture from tbselenium.test.test_addons import TBAddonsTest from xvfbwrapper import Xvfb

vdisplay = Xvfb(width=1920, height=1080, colordepth=24) vdisplay.start()

def start_tbselenium_with_adblock(): TBB_PATH = '/DEU/workers' # Replace with the actual TBB path xpi_path = '/DEU/workers/Browser/adblock_plus-3.17.xpi'

with TBDriverFixture(TBB_PATH, extensions=[xpi_path]) as driver:
    addons_test = TBAddonsTest()
    assert 'Adblock Plus' in addons_test.get_list_of_installed_addons(driver)
    # Run other test methods from TBAddonsTest if needed

if name == "main": start_tbselenium_with_adblock()`

addons_test.get_list_of_installed_addons(driver) throws the following exception *** selenium.common.exceptions.WebDriverException: Message: Failed to extract web element from Marionette response

SheldonPatnett commented 1 year ago

`from xvfbwrapper import Xvfb vdisplay = Xvfb(width=1920, height=1080, colordepth=24) vdisplay.start() from os.path import dirname, realpath, join from tbselenium.test.fixtures import TBDriverFixture from tbselenium.test.test_addons import TBAddonsTest import traceback

class CustomTBAddonsTest(TBAddonsTest): def init(self): super().init() self.TBB_PATH = 'workers'

def test_should_install_custom_extension(self):
    try:
        with TBDriverFixture(self.TBB_PATH, extensions=['workers/Browser/adblock_plus-3.17.xpi']) as driver:
            import ipdb ; ipdb.set_trace()
            assert 'Borderify' in self.get_list_of_installed_addons(driver)
    except Exception as e:
        print("Error occurred during test execution:")
        print(traceback.format_exc())

test = CustomTBAddonsTest() test.test_should_install_custom_extension() `

Sorry, I wasn't instancing the class properly in my previous post. I'm able to do things like driver.get("https://google.com") at the breakpoint but when I call the self.get_list_of_installed_addons(driver) method I'm getting *** selenium.common.exceptions.WebDriverException: Message: Failed to extract web element from Marionette response

gunesacar commented 1 year ago

It's hard to understand the code due to formatting issues, but if you can load a page via driver.get but you get an error for get_list_of_installed_addons, I'd recommend updating geckodriver to 0.31.0 and selenium to latest.

SheldonPatnett commented 1 year ago

I'm deeply sorry about the confusion. I really should learn Github formatting!

from xvfbwrapper import Xvfb
vdisplay = Xvfb(width=1920, height=1080, colordepth=24)
vdisplay.start()
from os.path import dirname, realpath, join
from tbselenium.test.fixtures import TBDriverFixture
from tbselenium.test.test_addons import TBAddonsTest
import traceback
from tbselenium.tbdriver import TorBrowserDriver
from selenium.webdriver.common.by import By

TBB_PATH = 'workers'
extensions = ['workers/Browser/adblock_plus-3.17.xpi', '/DEU/workers/Browser/ublock_origin-1.49.2.xpi']
driver = TorBrowserDriver("/DEU/workers", extensions=extensions)
driver.get("about:addons")
driver.save_screenshot('test.png')

Using snippets from your unit test, I was able to take a screenshot. The load_url method doesn't exists for some reason. The screenshot does indeed show the adblockers are installed, but I'm getting a low score on https://adblock-tester.com/ vs when I manually run it locally with extensions installed. This is most certainly out of scope for the this repository.

Thank you very much for you help :) Feel free to mark this issue as solved.

SheldonPatnett commented 1 year ago

P.S. Is there a way I can buy you a coffee?

SheldonPatnett commented 1 year ago

Maybe the extensions aren't able to run in the context of a private window? Which I believe TOR browsers launches in, by default?

gunesacar commented 1 year ago

Hi @SheldonPatnett , glad you have it worked.

Allowing extensions to run on Private windows is a good idea. Trying them one by one may be another... You can also compare the (adblocker) rulesets used in the browsers to see if the (ruleset) versions match or not.

But, as you said, this issue is likely not related to tor-browser-selenium. So I'm closing.