如何在python命令中进行多处理时关闭selenium驱动程序?

时间:2016-11-14 19:42:26

标签: python selenium multiprocessing

我写了一个python脚本,它使用多处理来打开四个不同的窗口并在它们中搜索一个常见的查询;我试图找到一种方法来关闭驱动程序而不是在函数结束时调用driver.close(),它会在成功生成后自动关闭驱动程序。

我想稍微浏览窗口并在完成后关闭它们。

以下是我的代码:

def main(hosts, inp):

    driver = webdriver.Chrome(executable_path='./chromedriver')
    driver.get(hosts)
    if 'ebay' in driver.current_url:
        print 'opened ebay'
        search_box = driver.find_element_by_id('gh-ac')
        search_box.clear()
        search_box.send_keys(inp)
        search_box.send_keys(Keys.RETURN)

    elif 'google' in driver.current_url:
        print 'opened google'
        search_box = driver.find_element_by_id('gbqfq')
        search_box.clear()
        search_box.send_keys(inp)
        search_box.send_keys(Keys.RETURN)

    elif 'etsy' in driver.current_url:
        print 'opened etsy'
        search_box = driver.find_element_by_id('search-query')
        search_box.clear()
        search_box.send_keys(inp)
        search_box.send_keys(Keys.RETURN)

    elif 'amazon' in driver.current_url:
        print 'opened amazon'
        search_box = driver.find_element_by_id('twotabsearchtextbox')
        search_box.clear()
        search_box.send_keys(inp)
        search_box.send_keys(Keys.RETURN)

    else:
        print "--NONE--"

    # driver.close()

if __name__ == '__main__':
    hosts = ["http://ebay.com", "https://www.google.com/shopping?hl=en", "http://etsy.com", "http://amazon.com"]
    num_hosts = len(hosts)
    inp = raw_input("What do you want to search?: ")
    p = Pool(num_hosts)
    partial_main = partial(main, inp=inp)
    p.map(partial_main, hosts)
    p.close()
    p.join()

我想做这样的事情:

done = raw_input("Type 'done' when finished:" )
    if done = 'done':
        driver.close()

但是刚注入main会给我一个EOF错误

0 个答案:

没有答案