Selenium - Firefox Options Not Taking Effect - python

I want to change web driver preferences so I can bypass the save/open prompt that pops up when the Firefox webdriver clicks on the download button for a pdf I want.
I am setting the preferences for the Firefox web driver and passing it as a parameter "options" when I initialize the webdriver. It shows that the preferences I enter save into options.preferences but when I have selenium click on the download button on the website, the download prompt for the pdf still pops up.
def __init__(self):
options = webdriver.FirefoxOptions()
options.headless = False
print(options.preferences)
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", 'myDir')
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
print(options.preferences)
self.driver = webdriver.Firefox(options = options)
I plan to have this headless, but have it set to False for now. What would be causing the webdriver not to take in the preferences I passed into Options?

Fixed the issue. It seems that the firefox pdf previewer was bypassing the settings I was passing through. The line below blocked the preview of the pdf and allowed the automatic download to take effect.
options.set_preference("pdfjs.disabled", True)

Related

How can I stop Selenium Firefox from opening a URL in a new window? (instead of a tab)

I use Selenium on Python and tried to use Firefox driver (as Chrome is too slow), but for some reasons Firefox driver always opens a new URL in a separate window, not a tab. Here is my driver initialization:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
options = webdriver.firefox.options.Options()
options.set_preference('intl.accept_languages', 'en-GB')
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_profile=profile, options=options)
And then it is just open each URL with window.open.
for url in page_lists:
driver.execute_script('window.open("{0}", "_blank");'.format(url))
This opens each URL in a new tab in Chrome driver, but how can I make Firefox driver do the same thing?
I tried adding these preferences according to this documentation, but none of them worked. In fact, the browser does not have a check-tick on the "open in a new tab" on the preferences page.
profile.set_preference("browser.link.open_newwindow", 3)
profile.set_preference("browser.link.open_newwindow.restriction", 0)
profile.set_preference("browser.link.open_external", 3)
profile.set_preference("browser.block.target_new_window", True)
On newer versions of selenium, you can use the following to open a new tab in the same window:
driver.switch_to.new_window("tab")
Then you would use the following to open a URL in that tab:
driver.get("URL")

How to change Firefox headless mode on the fly with Selenium?

I currently have my Selenium Firefox set up as so:
options = Options()
options.headless = True #hides browser
driver = webdriver.Firefox(options=options,executable_path=r'geckodriver.exe')
Which would make Firefox headless. However, I need it to display the browser window so that I can do some actions myself, and then switch it back to headless mode. Is there a way to do this, either through Firefox or Selenium, without restarting the driver?

Is there any way to interact with open external application alert from a webpage using selenium?

I tried switching tab that had popup using driver.switch_to.window(driver.window_handles[1])
and
closing the first tab, but none of them worked
This is the code that I tried to accept the popup driver.switch_to_alert().accept()
Have you tried disabling notifications through the chrome options?
This is what I had to use on my webscraping project:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-notifications')
prefs = {'profile.default_content_setting_values.notifications': 2}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Users\jeffg\Desktop\WebScraping\chromedriver.exe')
Documentation can be found here:
https://peter.sh/experiments/chromium-command-line-switches/
https://chromedriver.chromium.org/capabilities
It is not the usual javascript pop up. It is the browser pop up and you can't handle it with the code. I believe that you need to create a script with AutoIT to handle the same.

Pop Up Window with Selenium

I currently have a script that will log on to my company's wiki, visit a page, and select a download to pdf option available on the page. However, when this option is chosen, this dialogue box
pops up asking me to tell Firefox what to do with it. I just need selenium to interact and hit the "ok" button.
I'm not sure how to inspect this window for elements, and am need of direction. Any documentation helps.
from splinter import Browser
browser = Browser()
browser.visit('https://company.wiki.com')
browser.find_by_id('login-link').click()
browser.fill('os_username', 'user')
browser.fill('os_password', 'pass')
browser.find_by_name('login').click()
browser.visit('https://pageoncompany.wiki.com')
browser.find_by_xpath('//*[#id="navigation"]/ul/li[4]').click()
browser.find_by_id('action-export-pdf-link').click()
I was able to set the preferences through the web browser, then call my profile:
browser = Browser('firefox', profile=r'C:\Users\craab\AppData\Roaming\Mozilla\Firefox\Profiles\0lot9hun.default')
You can set preferences in order to prevent coming of download popup ad download it to pre-defined folder.
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2) # custom folder as set by repo
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", <download_folder_path>)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", content_type)
# Enable auto download, Avoid popup during downloads
fp.set_preference("browser.download.panel.shown", False)
fp.set_preference("browser.helperApps.neverAsk.openFile", content_type)
driver = webdriver.Firefox(fp)

how to close chrome browser popup dialog using selenium webdriver and python

I have a python code that uses selenium webdriver (along with chromedriver), to log into facebook and take a screenshot of the page.
the script itself works as expected, however, after logging in, chrome browser shows a dialog regarding facebook notifications (Chrome Popup)
The dialog seems to be generated by the browser, not the page itself.
i'm unable to identify and capture the element or use "switch to alert" method.
How can i interact with this dialog (either Allow, Dismiss or close it)?
Thanks!
in Python you can use
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-notifications")
webdriver.Chrome(os.path.join(path, 'chromedriver'),
chrome_options=chrome_options)
You can try with launching the chrome browser with disabled pop-ups (browser pop-ups). The following code snippet is in Java. It will be somewhat similar in python i believe.
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
options.addArguments("test-type");
ChromeDriver driver = new ChromeDriver(options);

Categories

Resources