OpenWPM icon indicating copy to clipboard operation
OpenWPM copied to clipboard

Problems with tab_restart_browser

Open turban1988 opened this issue 5 years ago • 16 comments

Hi

I found another problem with the tab_restart_browser function. The problem seems to be that spawning a new window fails due to the same origin policy. Sadly the problem does not occur deterministically. But identified websites on which the problem sometimes occurs (1) https://jalopnik.com/tag/ford and (2) https://jalopnik.com/volkswagen-thieves-almost-pull-off-the-perfect-train-he-1833936849#replies

This is the error I get

2019-04-10 16:42:09,575 - BrowserManager       - INFO     - BROWSER 1: Crash in driver, restarting browser manager 
 Traceback (most recent call last):
  File "/home/openwpm/new_try_ext_neu/OpenWPM/automation/BrowserManager.py", line 407, in BrowserManager
    browser_params, manager_params, extension_socket)
  File "/home/openwpm/new_try_ext_neu/OpenWPM/automation/Commands/command_executor.py", line 21, in execute_command
    extension_socket=extension_socket)
  File "/home/openwpm/new_try_ext_neu/OpenWPM/automation/Commands/browser_commands.py", line 156, in get_website
    tab_restart_browser(webdriver)
  File "/home/openwpm/new_try_ext_neu/OpenWPM/automation/Commands/browser_commands.py", line 138, in tab_restart_browser
    webdriver.execute_script("window.open('')")
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 543, in execute_script
    'args': converted_args})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Error: Permission denied to access property "_wrapped"

turban1988 avatar Apr 10 '19 14:04 turban1988

A try-catch clause does solve the problem for me.

    try:
        webdriver.execute_script("window.open('')")
    except WebDriverException as e:
        pass

This also resolves #279 .

However, the code migth fail at the assert statment. But that did not happen in my minimalistic tests. https://github.com/mozilla/OpenWPM/blob/e095344a1343cecd31be126941278efd0dae618c/automation/Commands/browser_commands.py#L106 Also, I am not quite aware of other consequenes that might occure if no new window/tab is spawned but the old one is used.

turban1988 avatar Apr 11 '19 08:04 turban1988

Thanks! I bet this website is overwriting or wrapping window.open, which can lead to a permission error.

I know we initially changed to this approach because of issues sending keyboard events to Firefox chrome after the upgrade to Selenium 3. I think it's worth revisiting those events to see if we're able to more reliably create and close windows.

englehardt avatar Apr 19 '19 17:04 englehardt

@englehardt can i work on this?

valkyr13 avatar Mar 20 '20 16:03 valkyr13

@valkyr13 Yes. I suggest first trying what we used to do before upgrading to Selenium 3. I wonder whether some of the unreliability has since been fixed.

englehardt avatar Mar 20 '20 17:03 englehardt

@valkyr13 are you still on this?

vringar avatar May 07 '20 20:05 vringar

@vringar May I work on this?

ankushduacodes avatar Dec 02 '20 13:12 ankushduacodes

Go ahead!

vringar avatar Dec 02 '20 15:12 vringar

@vringar I am sorry for the delays in the response to this issue. I was quite busy for the past two weeks and could not look into it much. I will be actively working on this issue and #758 from now on.

Thank you for your patience! :)

ankushduacodes avatar Dec 14 '20 08:12 ankushduacodes

Once again, no worries! :) Welcome back, I look forward to hearing from you again.

vringar avatar Dec 14 '20 13:12 vringar

@vringar As outlined in this comment by Steven, I tried to check if key combinations would work. Here's what I have tried so far to debug this issue(macOS specifics with geckodriver):

Serial no. Key combinations Status
1 CMD + c --Works
2 CMD + v --Works
3 CMD + t --Doesn't Work
4 CMD + w --Doesn't Work
5 CMD + SHFT + j --Doesn't Work
6 CMD + h --Doesn't Work
7 CMD + SHFT + h --Doesn't Work

Here's the code I was trying (using conda activate openwpm):

import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.webdriver import WebDriver


def main():
    driver: WebDriver = webdriver.Firefox(executable_path='/Users/ankushdua/Downloads/geckodriver')
    driver.get('http://www.mozilla.org')
    action = ActionChains(driver)
    action.key_down(Keys.COMMAND).send_keys('t').key_up(Keys.COMMAND).perform()
    time.sleep(4)
    driver.close()


if __name__ == '__main__':
    main()

What I understood:

The shortcuts/key combinations that try to manipulate the tab or whole browser window do not seem to work at all (I could not get any of the combinations from 3 to 7 did not work at all). I tried the same in Linux(Ubuntu) and got the same results.

Please let me know what could be the next step.


Update

As outlined in this StackOverflow answer, Selenium4 provides a new method to SwitchTo class:

https://github.com/SeleniumHQ/selenium/blob/941dc9c6b2e2aa4f701c1b72be8de03d4b7e996a/py/selenium/webdriver/remote/switch_to.py#L99-L111

Here is the usage to that method(from new docs):


# Opens a new tab and switches to new tab
driver.switch_to.new_window('tab')

# Opens a new window and switches to new window
driver.switch_to.new_window('window')

Now, Selenium4 is still in the development stage and has not released the new_window feature yet (Confirmed this twice by downloading the latest release source code). Do you want to wait for Selenium4 to be release before we tackle this issue or Do you have an alternative strategy for now?

ankushduacodes avatar Dec 15 '20 07:12 ankushduacodes

Thanks for the detailed writeup on this! Can you think of another way to open an about:blank tab and then closing all other windows and tabs?

vringar avatar Dec 15 '20 10:12 vringar

I am looking into it. I will update you soon.

ankushduacodes avatar Dec 15 '20 11:12 ankushduacodes

@vringar I was looking into emulating keypresses using javascript but could not make it happen :( Unfortunately, At this point, I am not aware of any other ways to open a new tab/window. I think for now window.open() is our best bet unless you have something in mind.

ankushduacodes avatar Dec 15 '20 12:12 ankushduacodes

Then we'll just leave this open until selenium releases a version with driver.switch_to._new_window() Thanks again for all of your research!

vringar avatar Dec 15 '20 15:12 vringar

@vringar While going through some extension APIs over at Bugzilla, I realized that it may be possible to load an extra extension that will solely be responsible for spawning a new browser using the windows API for extensions.

What do you think?

ankushduacodes avatar Mar 05 '21 20:03 ankushduacodes

I think we can wait this out and not introduce any more code on our side. Thanks for the suggestion though!

vringar avatar Mar 08 '21 16:03 vringar