0

This is one of my first attempt to write a python script-

I am trying to run a test case on localhost that is on IIS server. This is the local environment. I made following changes:

  1. Changed Firefox Settings to 'No proxy'
  2. Configured and redirected url in IIS such that '127.0.0.1' points to the website. So, entering 127.0.0.1 in browser redirects to the local website in IIS

I have to always 'Allow'/'Block' geoLocation alert and Firefox requires browser capabilities to set such.

Hence,

  1. I created a new Firefox profile
  2. Set it's preference such that is 'allows' alert for geoLocation services
  3. Set socks proxy to 127.0.0.1
  4. Set Desired Capabilities

Following is the code:

class CheckGeoLocationsDropDown():

        def test_check_geo_locations_dropdown(self):

            try:

                selenium_profile = webdriver.FirefoxProfile()


# media.navigator.permission.disabled is set in the FF to   True, however, as FF creates new Profile, do not know how to do. 

#selenium_profile.set_preference('media.navigator.permission.disabled', True)

                # Setting no proxy or socks proxy to 127.0.0.1 and port 80
                selenium_profile.set_preference("network.proxy.socks", '127.0.0.1')
                selenium_profile.set_preference("network.proxy.socks_port", 80)
                selenium_profile.update_preferences()

                baseUrl = 'http://127.0.0.1:80'
                # os.environ['no_proxy'] = '127.0.0.1:80'

                # Create a desired capabilities object as a starting point.
                capabilities = DesiredCapabilities.FIREFOX.copy()
                capabilities['platform'] ="WINDOWS"
                capabilities['version'] = "7"
                capabilities['nativeEvents'] = True
                capabilities['unexpectedAlertBehaviour'] = 'Accept'

                # Instantiate an instance of Remote WebDriver with the desired capabilities.
                driver = webdriver.Remote(desired_capabilities=capabilities,
                                          command_executor='http://127.0.0.1:80',
                                          browser_profile=selenium_profile)

              #driver.find_element_by_css_selector('#exceptionDialogButton')
                driver.maximize_window()

                driver.get(baseUrl)

        # driver.find_element_by_css_selector('#exceptionDialogButton')
        # _select_geo_locations_dropdown(driver)
        # print(driver.title)

                driver.implicitly_wait(5)
                driver.refresh()
                driver.close()

                item = '*'
                print(item * 40 + 'Geo Locations Dropdown selected successfully' + item * 40)

        except NoSuchElementException:
            print("Couldn't Run Script")

ff = CheckGeoLocationsDropDown()
ff.test_check_geo_locations_dropdown()

Below is the error I am getting enter image description here

I tried to read through Python document, however I am unable to find what is that I am doing wrong here. Please accept my apologies if I have not included enough information. Any help would be appreciated.

UPDATE 1/29/2018:

So I need to detect geolocation permission request alert. I should be able to allow/block the geolocation sharing. Since Selenium creates a new FF profile for every instance, I set the preference selenium_profile.set_preference('media.navigator.permission.‌​disabled', True)

I've now added to lines

capabilities['nativeEvents'] = True
capabilities['unexpectedAlertBehaviour'] = 'Accept'

It's still throwing the same error.

REFERENCES USED:

1 Answers1

0

It is tough to answer/recommend anything with the information you have provided interms of error stack trace. However I was able to reproduce the issue successfully.

If you look at the current implementation of selenium.webdriver.remote.webdriver it is defined as :

class selenium.webdriver.remote.webdriver.WebDriver(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False, file_detector=None, options=None)

If you look at the source code of Public Method set_preference(self, key, value) the following preferences are implemented which we can configure in the profile as :

self.default_preferences[key] = value

Preference List

  • set_preference("webdriver_firefox_port", self._port)
  • set_preference("webdriver_accept_untrusted_certs", value)
  • set_preference("webdriver_assume_untrusted_issuer", value)
  • set_preference("webdriver_enable_native_events", value)
  • set_preference("network.proxy.type", proxy.proxy_type['ff_value'])
  • set_preference("network.proxy.no_proxies_on", proxy.no_proxy)
  • set_preference("network.proxy.autoconfig_url", proxy.proxy_autoconfig_url)
  • set_preference("network.proxy.%s" % key, host_details[0])
  • set_preference("network.proxy.%s_port" % key, int(host_details2))

I didn't find a matching preference to the preference you mentioned as :

set_preference('media.navigator.permission.disabled', True)

Hence you see the error.


Update

As per your question update, as you need to detect geolocation permission and be able to allow/block the geolocation sharing you can add the following line of code :

opt.add_experimental_option("prefs", { \
    "profile.default_content_setting_values.geolocation": 1, 
  })

You can find a detailed discussion here.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352