Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在参数中传递Selenium Webdriver。在Python中可能吗?_Python_Selenium_Selenium Webdriver_Automated Tests_Parameter Passing - Fatal编程技术网

在参数中传递Selenium Webdriver。在Python中可能吗?

在参数中传递Selenium Webdriver。在Python中可能吗?,python,selenium,selenium-webdriver,automated-tests,parameter-passing,Python,Selenium,Selenium Webdriver,Automated Tests,Parameter Passing,我是Python新手 我的脚本的目的是使用SeleniumWebDriver解析网页。有两个子任务。我做的第一个任务没有错误。但所有脚本都没有任何功能 现在我想用一种有点复杂的方式来解决第二个任务。我想使用函数 问题是: 我可以在函数参数中传递活动的Selenium Webdriver吗? 第二个问题是: 如果答案是“是”-第二个函数是否也可以将活动的Webdriver传递给另一个函数? 因此,我考虑在不关闭会话的情况下激活Webdriver并将其传递到函数中 我的代码是: for key, v

我是Python新手

我的脚本的目的是使用SeleniumWebDriver解析网页。有两个子任务。我做的第一个任务没有错误。但所有脚本都没有任何功能

现在我想用一种有点复杂的方式来解决第二个任务。我想使用函数

问题是:

我可以在函数参数中传递活动的Selenium Webdriver吗?

第二个问题是:

如果答案是“是”-第二个函数是否也可以将活动的Webdriver传递给另一个函数?

因此,我考虑在不关闭会话的情况下激活Webdriver并将其传递到函数中

我的代码是:

for key, value in enumerate(headers):
    webdriver.DesiredCapabilities.PHANTOMJS[
        'phantomjs.page.customHeaders.{}'.format(key)] = value

w = webdriver.PhantomJS()

ps = q_yn('Perform new links parsing?', default='no')

if ps:
    csv_l = get_clist(w)

<...>

def get_clist(w):
    w.get(url)
    wait_pageload(w, 0)
    src = w.page_source
    BeautifulSoup(src, "lxml")
    ... (etc)

<..>

def wait_pageload(w, pt):
    if pt == 0:
        element = WebDriverWait(w, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, "serg_search_result")))
    elif pt == 1:
        element = WebDriverWait(w, 10).until(
            EC.presence_of_element_located((By.ID, "ya_share")))
    else:
        raise ValueError("Page type: %s is unknown" % pt)

是的,您可以将
WebDriver
作为参数传递给任何
函数
方法
。第二个问题的答案也是肯定的。如果您想这样做,可以在整个应用程序中共享一个
WebDriver

如果这不可能,一个非常流行的设计模式(
页面对象
)就不可能了。

谢谢,@RemcoW。所以这一切都应该起作用。例外情况来自Ubuntu 16.04中的PhantomJS错误描述
File "fts-crawl2.py", line 108, in wait_pageload
EC.presence_of_element_located((By.CLASS_NAME, "serg_s")))
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/support/
  wait.py", line 71, in until
  value = method(self._driver)

selenium.common.exceptions.WebDriverException: Message: Error - Unable
to load Atom 'find_element' from file ':/ghostdriver/./third_party/webdriver-
atoms/find_element.js'