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).