3

Here is the scenario/rabbit hole: I need to turn off the chrome pdfreader plugin so that pdfs automatically download rather than load in the reader. I am using ruby and watir. seems simple enough, the setting is located at: chrome://settings/content/pdfDocuments when using chrome browser.

(i have looked for switches, profile settings to do this on creation of the browser but found none)

if you look at the page, there is one button that I need to click.

here's the rub, it is buried in several layers of "shadow-roots" which makes it invisible. after hours of researching this, watir doesn't handle this very well.

I found a possible solution at: Shadow-dom support for selenium which looks like python.

I tried to simulate this in ruby as:

def expand_shadow_element(element, driver)
    shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
return shadow_root
end
root0 = driver.find_element :tag_name => 'settings-ui'
shadow_root0 = expand_shadow_element(root0, driver)
root1 = shadow_root0.find_element :tag_name => 'settings-subpage'
shadow_root1 = expand_shadow_element(root1, driver)
root2 = shadow_root1.find_element :tag_name => 'settings-pdf-documents'
shadow_root2 = expand_shadow_element(root2, driver)
root3 = shadow_root2.find_element :tag_name => 'settings-toggle-button'
shadow_root3 = expand_shadow_element(root3, driver)
root4 = shadow_root3.find_element :tag_name => 'paper-toggle-button'
shadow_root4 = expand_shadow_element(root4, driver)
toggleButton = shadow_root4.find_element :id => "toggleButton"
toggleButton.click

the def seems to work, however once it gets to defining "root1" it errors out with:

Selenium::WebDriver::Error::UnknownError: unknown error: b.getElementsByTagName is not a function

so basically 2 questions:

  1. Is there an easier way to turn off the pdf-reader plugin
  2. if not, and i've got to deal with the "shadow-dom" how do I go about opening the shadow-roots to see the elements so that I can manipulate them?

DM

Dieter
  • 83
  • 1
  • 5

1 Answers1

0

In the response where you're serving the file, set the content-disposition header to attachment and it should download. Example:

headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""

Jimmy Baker
  • 3,215
  • 1
  • 24
  • 26