1

I have been trying to insert javascript into Chrome navbar using Selenium, but without any success.

goto = "javascript:gotoText(-884)"<br />
browser.get(goto)

When done manually (by clicking and writing "javascript:gotoText(-884)" into the navbar) it works like a charm. However, the selenium brings me this error. Is there any workaround? The webpage itself provides nothing clickable that would lead directly to this link.

Thank you for any suggestions!

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-297-41c52a12ba91> in <module>()
----> 1 browser.get(asd)

/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in get(self, url)
    331         Loads a web page in the current browser session.
    332         """
--> 333         self.execute(Command.GET, {'url': url})
    334 
    335     @property

/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

WebDriverException: Message: unknown error: unsupported protocol
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.45.615355 (d5698f682d8b2742017df6c81e0bd8e6a3063189),platform=Mac OS X 10.13.6 x86_64)
Jan Pesl
  • 29
  • 5

2 Answers2

1

The error:

WebDriverException: Message: unknown error: unsupported protocol

Indicates you are using the browser.get() function wrong.

As you can see in the documentation-simple-usage (Python).

What you are trying to do is to inject JavaScript... (in Python you use: execute_script().

Here is an example of execute_script():

from selenium import webdriver

driver = webdriver.Chrome(executable_path=r'C:\path\To\chromedriver.exe')
driver.get("https://stackoverflow.com/questions/54132715/select-element-by-text-in-selenium/54132762#54132762")
driver.execute_script("document.getElementsByClassName('comment-user')[0].click()")

Hope this helps you!

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
1

Like Moshe Slavin mentioned, you need to pass the valid URL otherwise you will get this error :

WebDriverException: Message: unknown error: unsupported protocol

If you want pass some valid URL using the JavaScript like for example

http://www.google.com

then you can use window.location.replace() like below with JavaScriptExecutor in selenium with python, it behaves same as driver.get() method:

from selenium import webdriver
driver = webdriver.Chrome('C:\\NotBackedUp\\chromedriver.exe')
driver.execute_script("window.location.replace('http://www.google.com');")

For more info, refer the link below :

Ways to insert javascript into URL?

Ali
  • 1,689
  • 1
  • 5
  • 12