Python + Selenium WebDriver:在新标签页中打开URL

时间:2018-08-08 12:59:46

标签: python google-chrome selenium selenium-webdriver

我有一个文本文档,每行都有一个URL。我希望在一个新标签页中打开每个URL。这是我到目前为止的内容:

tabs = 0
f = open('links.txt', 'r', encoding='utf-8')
for line in f:
    url = line
    driver.execute_script("window.open(url, 'new_window')")
    sleep(7) # time to let tge page load
    tabs = tabs + 1 # to keep track of how many tabs I have

这给了我错误:

  

回溯(最近一次通话最后一次):文件“ scraper.py”,第100行,在          driver.execute_script(“ window.open(url,'new_window')”)文件“ C:\ Python37 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,   第635行,在execute_script中       'args':convert_args})['value']文件“ C:\ Python37 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,   第320行,执行       self.error_handler.check_response(响应)文件“ C:\ Python37 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py”,   第242行,在check_response中       引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:未知错误:   网址未定义

我已经尝试了所有方法,但无法正常工作。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

您试图在url中错误地传递execute_script变量。改为-

    tabs = 0
    f = open('links.txt', 'r', encoding='utf-8')
    for line in f:
        url = line
        driver.execute_script("window.open(arguments[0])", url)
        sleep(7) # time to let tge page load
        tabs = tabs + 1 # to keep track of how many tabs I have

arguments[0]是指您的网址。