0

I would like to detect when some javascript wants to open a new tab so I could force it to open it in the current tab or at least get the URL to load it in my current tab.

I am NOT talking about removing a target="_blank" attribute, since I have no problem handling this case.

context: the reason why I want to do so is because in some cases when I run selenium in headless mode on my Ubuntu server, switching tab using driver.switch_to runs for an unlimited time, eventually yielding absolutely nothing (which is apparently a well known bug).

for this reason it would be fantastic if I could do any of these two things:

  • intercept the url of a tab my main window is about to open from javascript

or alternatively:

  • get the url of a tab I am not on with Selenium but without calling the switch_to method.

Has someone ever somehow found a fix to this problem either while developing a browser extension or while using an automation framework ?

EDIT since some comments are leading to the setup of my Selenium webdriver, here it is :

chrome_options = OptionsChrome()
chrome_options.add_experimental_option("prefs", {
   "download.default_directory": download_directory,
})
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(
  executable_path=path_to_driver,
  chrome_options=chrome_options
)
params = {'behavior': 'allow', 'downloadPath': download_directory}
driver.execute_cdp_cmd('Page.setDownloadBehavior', params)

I am using ChromeDriver 79.0.3945.16, Google Chrome 79.0.3945.130, Selenium 3.141.0 on Ubuntu 18.04. It might be worth noting that it works perfectly to download pdf when clicking on link linking to a pdf (but not if the .pdf is called by some javascript).

romainm
  • 281
  • 8
  • 16
  • You can probably inject js that overrides `window.open` function or simply reassign it to location.replace. – wOxxOm Jan 17 '20 at 17:07
  • I tried to do this but it didn't work, I guess it is not overriding the method globally on all scripts of the page... – romainm Jan 17 '20 at 17:22
  • The page may remember the initial value of window.open if you override it too late, or the page may be calling it from an iframe. Anyway I see there's an automation event [Page.windowOpen](https://chromedevtools.github.io/devtools-protocol/tot/Page/#event-windowOpen) which in the worst case should be available as a raw debugger command in new Selenium, but hopefully there's a more high-level way to use it, see the documentation. P.S. As for an extension, it would have to override window.open in the [page context](https://stackoverflow.com/a/9517879). – wOxxOm Jan 17 '20 at 17:49
  • Here is how i tried to override the default `window.open` function: `var windowOpenBackup = window.open; window.open = function(url, target) {windowOpenBackup(url,'_self')};` I injected the code from contentscript (in main window and in all iframes as well) in an extension and also tried immediately after each page load in selenium, the code is correctly injected but it doesn't work... I don't know how to work with Page.windowOpen but I will try to figure this out. Or maybe there are other functions than `window.open` ? for instance in my case it open a new tab to download a .xls file. – romainm Jan 17 '20 at 18:12
  • Might be worth noting that if the new tab has nothing but an .xls file Selenium will not be able to do anything with that content. (Though it can close the tab/window or navigate to a new URL) Posting the script that opens the tab would be helpful. – pcalkins Jan 17 '20 at 18:52
  • yes, I would just like to download it. – romainm Jan 17 '20 at 19:15
  • The site may not be sending download request headers... so it's up to the browser what it does... depending on the filetype it may try to open it. Once you have the URL (assuming it is to the file) you can download it using native code. For instance Java has copyURLToFile: https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL,%20java.io.File) – pcalkins Jan 17 '20 at 22:10
  • Another issue might be headless mode failing because the browser is set to prompt. You can set options in the webdriver for setting the download dir and whether to prompt or not. For instance Chrome headless: https://stackoverflow.com/questions/57599776/download-file-through-google-chrome-in-headless-mode – pcalkins Jan 17 '20 at 22:16
  • Ok, I will be trying this, and coming back with how it unfolds in the following hours. – romainm Jan 18 '20 at 09:03

0 Answers0