【问题标题】:Selenium - run javascript via navbar using chromedriverSelenium - 使用 chromedriver 通过导航栏运行 javascript
【发布时间】:2019-06-29 13:18:22
【问题描述】:

我一直在尝试使用 Selenium 将 javascript 插入 Chrome 导航栏,但没有任何成功。

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

手动完成时(通过单击并将“javascript:gotoText(-884)”写入导航栏),它就像一个魅力。但是,硒给我带来了这个错误。 有什么解决方法吗?该网页本身没有提供任何可直接指向此链接的可点击内容。

感谢您的任何建议!

---------------------------------------------------------------------------
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)

【问题讨论】:

    标签: python selenium web-scraping selenium-chromedriver


    【解决方案1】:

    错误:

    WebDriverException:消息:未知错误:不支持的协议

    说明你使用了browser.get()函数错误。

    正如您在documentation-simple-usage (Python) 中看到的那样。

    您要做的是inject JavaScript...(在 Python 中您使用:execute_script()

    这里是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()")
    

    希望对你有帮助!

    【讨论】:

    • 太棒了! “execute_script(goto)”——这正是我所需要的!谢谢大佬!
    • 很高兴为@JanPesl 提供帮助!
    • @JanPesl Upvote 或接受答案,如果此/任何答案对您/对您有帮助,以造福未来的读者。见:stackoverflow.com/help/why-vote
    • 高效使用execute_script()
    【解决方案2】:

    就像 Moshe Slavin 提到的,你需要传递有效的 URL,否则你会得到这个错误:

    WebDriverException: Message: unknown error: unsupported protocol
    

    如果您想使用 JavaScript 传递一些有效的 URL,例如

    http://www.google.com

    然后你可以使用 window.location.replace() 像下面的 JavaScriptExecutor 在 selenium 和 python 中,它的行为与 driver.get() 方法相同:

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

    欲了解更多信息,请参阅以下链接:

    Ways to insert javascript into URL?

    【讨论】:

    • 很好地补充了我的答案!
    猜你喜欢
    • 2015-03-12
    • 2019-08-06
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2014-10-07
    • 2017-04-24
    • 2019-02-28
    相关资源
    最近更新 更多