doctordrue
doctordrue

Reputation: 244

Allow Flash content in Chrome 69 running via chromedriver

Does anybody know how to enable Flash plugin in Chrome 69. I use chromedriver 2.41 with java selenium bindings. I've tried with

prefs.put("profile.default_content_setting_values.plugins", 1);
prefs.put("profile.content_settings.plugin_whitelist.adobe-flash-player", 1);
prefs.put("profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player", 1);

but with no luck. I've also tried to compare chrome profile preferences with disallowed/allowed flash for particular site and then tried with:

            Map<String, Object> site = new HashMap<>();
            Map<String, Object> values = new HashMap<>();
            Map<String, Object> setting = new HashMap<>();
            setting.put("flashPreviouslyChanged", true);
            values.put("last_modified", "13180613213099316");
            values.put("setting", setting);
            site.put("http://my.site,*", values);
            prefs.put("profile.content_settings.exceptions.flash_data", site);

but it won't work as well.

I've also tried to run with profile specified via

options.addArguments("user-data-dir=" + profileDir);

but since this white list setting becomes 'ephemeral' in Chrome 69 it also won't work.

Is there any method to run my automation in Chrome with flash support?

Upvotes: 9

Views: 11133

Answers (15)

paolov
paolov

Reputation: 2337

I have the same answer as user BaiJiFeiLong

but i found that I had to change the line:

root5 = shadow_root4.find_element(By.ID, "advancedPage")

to:

root5 = shadow_root4.find_element(By.ID, "basicPage")

Because the original line was returning NoSuchElement error.

Upvotes: 0

SagRU
SagRU

Reputation: 448

Since EnableEphemeralFlashPermission can no longer be disabled in Chrome 71+ versions, here's a slightly modified version of @JohnoCrawford code that works for Chrome 81:

def add_flash_site(driver, web_url):
    def expand_root_element(element):
        return driver.execute_script("return arguments[0].shadowRoot", element)

    driver.get("chrome://settings/content/siteDetails?site=" + web_url)
    root1 = driver.find_element(By.TAG_NAME, "settings-ui")
    shadow_root1 = expand_root_element(root1)
    root2 = shadow_root1.find_element(By.ID, "container")
    root3 = root2.find_element(By.ID, "main")
    shadow_root3 = expand_root_element(root3)
    root4 = shadow_root3.find_element(By.CLASS_NAME, "showing-subpage")
    shadow_root4 = expand_root_element(root4)
    root5 = shadow_root4.find_element(By.TAG_NAME, "settings-privacy-page")
    shadow_root5 = expand_root_element(root5)
    root6 = shadow_root5.find_element(By.ID, "pages")
    root7 = root6.find_element(By.TAG_NAME, "settings-subpage")
    root8 = root7.find_element(By.TAG_NAME, "site-details")
    shadow_root8 = expand_root_element(root8)
    root9 = shadow_root8.find_element(By.ID, "plugins")
    shadow_root9 = expand_root_element(root9)
    root10 = shadow_root9.find_element(By.ID, "permission")
    Select(root10).select_by_value("allow")

Upvotes: 0

BaiJiFeiLong
BaiJiFeiLong

Reputation: 4665

Python3 version for Chrome 74. Converted from the Java version of @JohnoCrawford above.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

def add_flash_site(driver, web_url):
    def expand_root_element(element):
        return driver.execute_script("return arguments[0].shadowRoot", element)

    driver.get("chrome://settings/content/siteDetails?site=" + web_url)
    root1 = driver.find_element(By.TAG_NAME, "settings-ui")
    shadow_root1 = expand_root_element(root1)
    root2 = shadow_root1.find_element(By.ID, "container")
    root3 = root2.find_element(By.ID, "main")
    shadow_root3 = expand_root_element(root3)
    root4 = shadow_root3.find_element(By.CLASS_NAME, "showing-subpage")
    shadow_root4 = expand_root_element(root4)
    root5 = shadow_root4.find_element(By.ID, "advancedPage")
    root6 = root5.find_element(By.TAG_NAME, "settings-privacy-page")
    shadow_root6 = expand_root_element(root6)
    root7 = shadow_root6.find_element(By.ID, "pages")
    root8 = root7.find_element(By.TAG_NAME, "settings-subpage")
    root9 = root8.find_element(By.TAG_NAME, "site-details")
    shadow_root9 = expand_root_element(root9)
    root10 = shadow_root9.find_element(By.ID, "plugins")
    shadow_root10 = expand_root_element(root10)
    root11 = shadow_root10.find_element(By.ID, "permission")
    Select(root11).select_by_value("allow")

Upvotes: 2

user6470139
user6470139

Reputation:

Enable flash in chromedriver using robotframework

Credits: @BaiJiFeiLong

Save following code as flash_helper.py

from robot.libraries.BuiltIn import BuiltIn
from selenium.webdriver.common.by import By as WebBy
from selenium.webdriver.support.ui import Select as WebSelect

def allow_flash(url):

  seleniumlib = BuiltIn().get_library_instance('SeleniumLibrary')
  driver = seleniumlib.driver

  def _shadow_root(driver, element):
    return driver.execute_script("return arguments[0].shadowRoot", element)

  driver.get("chrome://settings/content/siteDetails?site={}".format(url))

  root1 = driver.find_element(WebBy.TAG_NAME, "settings-ui")
  shadow_root1 = _shadow_root(driver, root1)
  root2 = shadow_root1.find_element(WebBy.ID, "container")
  root3 = root2.find_element(WebBy.ID, "main")
  shadow_root3 = _shadow_root(driver, root3)
  root4 = shadow_root3.find_element(WebBy.CLASS_NAME, "showing-subpage")
  shadow_root4 = _shadow_root(driver, root4)
  root5 = shadow_root4.find_element(WebBy.ID, "advancedPage")
  root6 = root5.find_element(WebBy.TAG_NAME, "settings-privacy-page")
  shadow_root6 = _shadow_root(driver, root6)
  root7 = shadow_root6.find_element(WebBy.ID, "pages")
  root8 = root7.find_element(WebBy.TAG_NAME, "settings-subpage")
  root9 = root8.find_element(WebBy.TAG_NAME, "site-details")
  shadow_root9 = _shadow_root(driver, root9)
  root10 = shadow_root9.find_element(WebBy.ID, "plugins")  # Flash
  shadow_root10 = _shadow_root(driver, root10)
  root11 = shadow_root10.find_element(WebBy.ID, "permission")
  WebSelect(root11).select_by_value("allow")

Use above method as keyword in robotframework

save following code as test.robot

*** Settings ***
Library    SeleniumLibrary
Library    flash_helper.py

*** Test Case ***
Allow Flash In Chrome
    Open Browser    https://www.google.com    chrome

    # go to chrome settings and enable flash
    ${CURRENT_URL}    Get Location
    Allow Flash    ${CURRENT_URL}

    # revert to previous page
    Go To   ${CURRENT_URL}

   # now Flash is enabled in chrome!!

Upvotes: 0

Martian
Martian

Reputation: 11

Since I saw many methods here is not work for Chrome 71, I would like to share the solution in C# that I am using:

ChromeOptions chromeOptions = new ChromeOptions();
List<string> allowFlashUrls = new List<string>() {
    "*.testing1.com",
    "*.testing2.com",
};
chromeOptions.AddUserProfilePreference("profile.managed_plugins_allowed_for_urls", config.ChromeConfig.AllowFlashUrls);
ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
// Then run your test using chromeDriver

By setting profile.managed_plugins_allowed_for_urls will force Chrome the allow run Flash in the domain declare in allowFlashUrls list. Not tested but it should allow Flash to all site by adding http://* and https:// to allow Flash list.

Upvotes: 1

majia29
majia29

Reputation: 11

thanks for @JohnoCrawford, i wrote a python code by referring to his java code.

from urllib import quote_plus as url_quoteplus
from urlparse import urlsplit
from selenium.webdriver.common.by import By as WebBy
from selenium.webdriver.support.ui import Select as WebSelect

    def allow_flash(driver, url):
        def _base_url(url):
            if url.find("://")==-1:
                url = "http://{}".format(url)
            urls = urlsplit(url)
            return "{}://{}".format(urls.scheme, urls.netloc)

        def _shadow_root(driver, element):
            return driver.execute_script("return arguments[0].shadowRoot", element)

        base_url = _base_url(url)
        driver.get("chrome://settings/content/siteDetails?site={}".format(url_quoteplus(base_url)))

        root1 = driver.find_element(WebBy.TAG_NAME, "settings-ui")
        shadow_root1 = _shadow_root(driver, root1)
        root2 = shadow_root1.find_element(WebBy.ID, "container")
        root3 = root2.find_element(WebBy.ID, "main")
        shadow_root3 = _shadow_root(driver, root3)
        root4 = shadow_root3.find_element(WebBy.CLASS_NAME, "showing-subpage")
        shadow_root4 = _shadow_root(driver, root4)
        root5 = shadow_root4.find_element(WebBy.ID, "advancedPage")
        root6 = root5.find_element(WebBy.TAG_NAME, "settings-privacy-page")
        shadow_root6 = _shadow_root(driver, root6)
        root7 = shadow_root6.find_element(WebBy.ID, "pages")
        root8 = root7.find_element(WebBy.TAG_NAME, "settings-subpage")
        root9 = root8.find_element(WebBy.TAG_NAME, "site-details")
        shadow_root9 = _shadow_root(driver, root9)
        root10 = shadow_root9.find_element(WebBy.ID, "plugins")  # Flash
        shadow_root10 = _shadow_root(driver, root10)
        root11 = shadow_root10.find_element(WebBy.ID, "permission")
        WebSelect(root11).select_by_value("allow")

Upvotes: 1

Johno Crawford
Johno Crawford

Reputation: 476

Given the flag --disable-features=EnableEphemeralFlashPermission has been removed in Chrome 71 which severely cripples Flash test automation I would like to share our solution.

public class FlashPolicyHelper {

    private final ChromeDriver driver;

    public FlashPolicyHelper(ChromeDriver driver) {
        this.driver = driver;
    }

    public FlashPolicyHelper addSite(String site) {
        this.driver.get("chrome://settings/content/siteDetails?site=" + site);
        WebElement root1 = driver.findElement(By.tagName("settings-ui"));
        WebElement shadowRoot1 = expandRootElement(root1);
        WebElement root2 = shadowRoot1.findElement(getByIdentifier("id=container"));
        WebElement main = root2.findElement(getByIdentifier("id=main"));
        WebElement shadowRoot3 = expandRootElement(main);
        WebElement shadowRoot4 = shadowRoot3.findElement(getByIdentifier("class=showing-subpage"));
        WebElement shadowRoot5 = expandRootElement(shadowRoot4);
        WebElement shadowRoot6 = shadowRoot5.findElement(getByIdentifier("id=advancedPage"));
        WebElement shadowRoot7 = shadowRoot6.findElement(By.tagName("settings-privacy-page"));
        WebElement shadowRoot8 = expandRootElement(shadowRoot7);
        WebElement shadowRoot9 = shadowRoot8.findElement(getByIdentifier("id=pages"));
        WebElement shadowRoot10 = shadowRoot9.findElement(By.tagName("settings-subpage"));
        WebElement shadowRoot11 = shadowRoot10.findElement(By.tagName("site-details"));
        WebElement shadowRoot12 = expandRootElement(shadowRoot11);
        WebElement shadowRoot13 = shadowRoot12.findElement(By.id("plugins"));
        WebElement shadowRoot14 = expandRootElement(shadowRoot13);
        new Select(shadowRoot14.findElement(By.id("permission"))).selectByValue("allow");
        return this;
    }

    private By getByIdentifier(String identifier) {
        String[] identifiers = identifier.split("=");

        return identifiers[0].equals("id") ? By.id(identifiers[1]) :
                By.className(identifiers[1]);
    }

    private WebElement expandRootElement(WebElement element) {
        return (WebElement) driver.executeScript("return arguments[0].shadowRoot",element);
    }
}

The helper should be called after instantiating the ChromeDriver.

 driver = new ChromeDriver(options);
 new FlashPolicyHelper(driver).addSite("https://your.site").addSite("https://another.site");

Upvotes: 3

陈俊高
陈俊高

Reputation: 1

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-features=EnableEphemeralFlashPermission");
    Map<String, Object> prefs = new HashMap<>();
    prefs.put("profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player",1);
    options.setExperimentalOption("prefs", prefs);
    WebDriver driver = new ChromeDriver(options);
    driver.get("some url");

Upvotes: 0

Nathan Friedly
Nathan Friedly

Reputation: 8166

In case anyone else needs it, here's how to do the same thing in a Protractor config:

capabilities: {
  browserName: 'chrome',
  chromeOptions: {
    args: ['--disable-features=EnableEphemeralFlashPermission'],
    prefs: {
      "profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player": 1,
    }
  },
}

Upvotes: 0

Jorge Augusto
Jorge Augusto

Reputation: 1

Using @RodolphoSilva answer with links:

1 - Input link: chrome://flags/#enable-ephemeral-flash-permission

2 - Change to "Disabled"

3 - Click "RELAUNCH NOW" button

4 - Input link: chrome://settings/content/flash?search=flash

5 - Now you can add or block sites to use flash

@RodolphoSilva - Many thanks for your great answer!

Upvotes: 0

Philipp  Moskalev
Philipp Moskalev

Reputation: 1

My solution for C#

var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--disable-features=EnableEphemeralFlashPermission");
chromeOptions.AddUserProfilePreference(
          "profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player", 1);
var capability = (DesiredCapabilities)chromeOptions.ToCapabilities();

Upvotes: 0

doctordrue
doctordrue

Reputation: 244

Thanks everyone for answers.

I finally have found the solution. In order to enable flash progrmatically since Chrome 69 we have to do 2 things:

  1. Disable Ephemeral Flash Permissions (to enable list of allowed for Flash sites) and
  2. Add all sites to that list.

See the following code on Java:

ChromeOptions options = new ChromeOptions();
// disable ephemeral flash permissions flag
options.addArguments("--disable-features=EnableEphemeralFlashPermission");
Map<String, Object> prefs = new HashMap<>();
// Enable flash for all sites for Chrome 69
prefs.put("profile.content_settings.exceptions.plugins.*,*.setting", 1);

options.setExperimentalOption("prefs", prefs);
nestedDriver = new ChromeDriver(options);

Upvotes: 3

Rodolpho Silva
Rodolpho Silva

Reputation: 29

Follow these steps:

  1. Input this URL in Chrome: chrome://flags/
  2. On search inputbox, digit: ephemeral flash
  3. Choose "disabled" option.

This will not ask to run Flash Player for further sessions in Chrome 69.

Upvotes: 2

CQ Bear
CQ Bear

Reputation: 394

Chrome 69 recently released does not allow sites (URLs) to be permanently added (enabled) via chrome://settings/content/flash as was the case for previous versions of Flash Player. However, a URL can be temporarily enabled for the current sesison by clicking on the lock icon to the left of the location bar, then choose Site Settings, and then enable Flash Player.

This policy forces users of Flash Player to re-configure their permission settings every session, which makes it less convenient to use Flash Player. That is apparently by design.

Fortunately, the Microsoft Edge browser does not have this policy. Like Chrome, Edge runs Flash Player. However, unlike Chrome, it persists the permission settings and does not inconvenience the user.

Upvotes: 1

JeffH
JeffH

Reputation: 1

I handled this by going into the settings for the website before I ran my test and doing selenium actions such as below:

     public void SetFlashForURL (string yourWebsiteURL) {
 driver.Navigate().GoToUrl(string.Format("chrome://settings/content/siteDetails?site={0}", yourWebsiteURL));
                    Thread.Sleep(1000);
                    Actions actions = new Actions(driver);
                    if (yourWebsiteURL.Contains("https"))
                    {
                        actions.SendKeys(OpenQA.Selenium.Keys.Tab);
                        actions.SendKeys(OpenQA.Selenium.Keys.Tab);
                        actions.SendKeys(OpenQA.Selenium.Keys.Tab);
                        actions.SendKeys(OpenQA.Selenium.Keys.Tab);
                    }
                    actions.SendKeys(OpenQA.Selenium.Keys.Tab);
                    actions.SendKeys(OpenQA.Selenium.Keys.Tab);
                    actions.SendKeys(OpenQA.Selenium.Keys.Tab);
                    actions.SendKeys(OpenQA.Selenium.Keys.Down);
                    actions.Build().Perform(); 
    }

Upvotes: 0

Related Questions