我是Python的新手,我正在尝试从robotframwork修改SeleniumLibrary以满足我自己的需求。不幸的是,我遇到了多个错误,我不知道为什么。
使用二进制路径和无头选项调用不同浏览器有多种功能。
import os
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
DEFAULT_BROWSR = 'chrome'
DRIVERS_PATH = os.path.join( os.getcwd() , "\drivers\\" )
BROWSER_NAMES = {
'ff': "_make_ff",
'firefox': "_make_ff",
'headlessfirefox': '_make_headless_ff',
'gc': "_make_chrome",
'googlechrome': "_make_chrome",
'headlesschrome': '_make_headless_chrome',
'chrome': "_make_chrome",
}
BROWSER_PATHS = {
'_make_ff': "geckodriver-win32\geckodriver.exe",
'_make_headless_ff': "geckodriver-win32\geckodriver.exe",
'_make_chrome': "chromedriver_win32\chromedriver.exe",
'_make_headless_chrome': "chromedriver_win32\chromedriver.exe",
}
class driver(object):
"""docstring for driver"""
def __init__(self):
self.driver = None
self.driver_path = None
self.timeout = 20
self.implicit_wait = 5
def create(self,browser_name=DEFAULT_BROWSR):
return self._make_driver(browser_name)
def _make_driver(self, browser_name, desired_capabilities=None, profile_dir=None, remote=None):
creation_func = self._get_driver_creation_function(browser_name)
driver_path = self._get_driver_path(browser_name)
desired_capabilities = {"chrome.binary": driver_path,}
driver = creation_func(remote, desired_capabilities, profile_dir)
driver.set_script_timeout(self.timeout)
driver.implicitly_wait(self.implicit_wait)
return driver
""" Return browser function name """
def _get_driver_creation_function(self,browser_name):
try:
func_name = BROWSER_NAMES[browser_name]
except KeyError:
raise ValueError(browser_name + " is not a supported browser.")
return getattr(self, func_name)
""" Return browser driver path """
def _get_driver_path(self,browser_name):
try:
func_name = self._get_driver_creation_function(browser_name)
driver_path = os.path.join(DRIVERS_PATH , BROWSER_PATHS[func_name])
except KeyError:
raise ValueError(browser_name + " is not a supported browser.")
return getattr(self, driver_path)
""" Used inside driver creation functions """
def _generic_make_driver(self, webdriver_type, desired_cap_type, remote_url, desired_caps, options=None):
if is_falsy(remote_url):
if options is None:
driver = webdriver_type()
else:
driver = webdriver_type(options=options)
else:
driver = self._create_remote_web_driver(desired_cap_type, remote_url, desired_caps, options=options)
return driver
def _create_remote_web_driver(self, capabilities_type, remote_url, desired_capabilities=None, profile=None, options=None):
desired_capabilities_object = capabilities_type.copy()
if not isinstance(desired_capabilities, dict):
desired_capabilities = self._parse_capabilities_string(desired_capabilities)
desired_capabilities_object.update(desired_capabilities or {})
return webdriver.Remote(desired_capabilities=desired_capabilities_object,
command_executor=str(remote_url), browser_profile=profile,
options=options)
def _make_ff(self, remote, desired_capabilities, profile_dir, options=None):
if is_falsy(profile_dir):
profile = webdriver.FirefoxProfile()
else:
profile = webdriver.FirefoxProfile(profile_dir)
if is_truthy(remote):
driver = self._create_remote_web_driver(webdriver.DesiredCapabilities.FIREFOX, remote,
desired_capabilities, profile, options=options)
else:
driver = webdriver.Firefox(firefox_profile=profile, options=options, **self._geckodriver_log_config)
return driver
def _make_headless_ff(self, remote, desired_capabilities, profile_dir):
options = webdriver.FirefoxOptions()
options.set_headless()
return self._make_ff(remote, desired_capabilities, profile_dir, options=options)
def _make_chrome(self, remote, desired_capabilities, profile_dir, options=None):
return self._generic_make_driver(
webdriver.Chrome, webdriver.DesiredCapabilities.CHROME, remote,
desired_capabilities, options=options)
def _make_headless_chrome(self, remote, desired_capabilities, profile_dir):
options = webdriver.ChromeOptions()
options.set_headless()
return self._make_chrome(remote, desired_capabilities, profile_dir, options)
此功能应该打开一个浏览器,调用默认的chrome驱动程序:
driver().create()
答案 0 :(得分:0)
正如评论中所强调的那样,通过使用具有调试功能的正确配置的Python IDE,可以轻松解决此类问题。有几个好的,包括开源和商业。
在这种特殊情况下,您的脚本有两个问题:
缺少导入:
from robot.utils import is_falsy
需要稍微重写的函数,因为func_name
需要是不同的值。
""" Return browser driver path """
def _get_driver_path(self,browser_name):
try:
#func_name = self._get_driver_creation_function(browser_name)
driver_path = os.path.join(DRIVERS_PATH , BROWSER_PATHS[BROWSER_NAMES[browser_name]])
except KeyError:
raise ValueError(browser_name + " is not a supported browser.")
return driver_path