【问题标题】:Unable to run selenium tests on chrome locally on Mac无法在 Mac 上本地运行 chrome 上的 selenium 测试
【发布时间】:2017-01-24 19:30:17
【问题描述】:

我正在尝试在 Mac(El Capitan) 上本地运行 chrome 测试,但是我无法在 chrome 上本地运行测试,我可以使用远程 webdriver 调用 chrome。但是当我尝试在没有远程 webdriver 的情况下运行测试时,即直接调用 chrome,我得到了错误

    driver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/Users/testuser/practise/venv/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 92, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
WebDriverException: Message: Can not connect to the Service chromedriver

我的 chromedriver 二进制文件位于 /usr/local/bin,我的 PATH 中也有相同的文件。

Selenium Version: selenium (2.53.6)
Chromedriver Version: 2.24.417412
Python Version: 2.7.10

我的示例脚本

# Import unittest module for creating unit tests
import unittest

# Import time module to implement
import time

# Import the Selenium 2 module (aka "webdriver")
from selenium import webdriver

# For automating data input
from selenium.webdriver.common.keys import Keys

# For providing custom configurations for Chrome to run
from selenium.webdriver.chrome.options import Options


# --------------------------------------
# Provide a class for the unit test case
class PythonOrgSearchChrome(unittest.TestCase):

    # Anything declared in setUp will be executed for all test cases
    def setUp(self):
        # Select which device you want to emulate by uncommenting it
        # More information at: https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation
        mobile_emulation = {

            "deviceName": "Google Nexus 5"
            # Or specify a specific build using the following two arguments
            #"deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
            #"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
        }

        # Define a variable to hold all the configurations we want
        chrome_options = webdriver.ChromeOptions()

        # Add the mobile emulation to the chrome options variable
        chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

        # Create driver, pass it the path to the chromedriver file and the special configurations you want to run
        self.driver = webdriver.Chrome(chrome_options=chrome_options)

    # An individual test case. Must start with 'test_' (as per unittest module)
    def test_search_in_python_chrome(self):
        # Assigning a local variable for the global driver
        driver = self.driver

        # Go to google.com
        driver.get('http://www.google.com')

        # A test to ensure the page has keyword Google in the page title
        self.assertIn("Google", driver.title)



        # Find and select the search box element on the page
        search_box = driver.find_element_by_name('q')

        # Enter text into the search box
        search_box.send_keys('Cat gif')

        # Make sure the results page returned something
        assert "No results found." not in driver.page_source

        # Submit the search box form
        search_box.submit()

        # Can also use Keys function to submit
        #search_box.send_keys(Keys.RETURN)

        # Another pause so we can see what's going on
        time.sleep(5)

        # Take a screenshot of the results
        driver.save_screenshot('screenshot-deskto-chrome.png')

    # Anything declared in tearDown will be executed for all test cases
    def tearDown(self):
        # Close the browser.
        # Note close() will close the current tab, if its the last tab it will close the browser. To close the browser entirely use quit()
        self.driver.close()

# Boilerplate code to start the unit tests
if __name__ == "__main__":
    unittest.main()

我真的不确定发生了什么,因为我可以使用远程 webdriver 启动浏览器,任何帮助将不胜感激。

【问题讨论】:

  • 你用最新的 chromedriver 试过最新的 selenium 了吗?为避免与路径相关的任何问题,您可以将它们(selenium 和 chromedriver)保存在同一目录中并从那里运行。如果浏览器会自动更新,有时您可能会在 chromedriver 和 chrome 浏览器之间遇到不兼容问题。
  • AFAIK 正在运行最新版本的 chromedriver 和 selenium。当我使用远程 webdriver 调用 chrome 时,同样的工作正常。如果是路径问题,那么远程 webdriver 也应该抱怨
  • 我认为最新的 selenium 是 3.0。请重新检查并在此处查看我关于如何使用 chromedriver stackoverflow.com/questions/39468600/…启动 selenium 的答案@

标签: macos python-2.7 google-chrome selenium


【解决方案1】:

我想我找到了答案,在我的/etc/hosts localhost 被映射到其他东西,结果我得到了

driver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/Users/testuser/practise/venv/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 92, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
WebDriverException: Message: Can not connect to the Service chromedriver

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多