2017-06-12 67 views
0

问题描述

我正在使用Ubuntu 16.04。 我想从网站下载CSV文件。他们通过链接呈现。当我点击链接时,我想打开一个新的标签,它将下载该文件。我使用了https://gist.github.com/lrhache/7686903中提供的解决方案。无法打开新选项卡的硒以下载CSV文件python

 image description here


设置
fp = webdriver.FirefoxProfile() 
    fp.set_preference("browser.download.folderList",2) 
    fp.set_preference("browser.download.manager.showWhenStarting",False) 
    fp.set_preference("browser.download.dir",download_path) 
    fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv") 

    # create a selenium webdriver 
    browser = webdriver.Firefox(firefox_profile=fp) 
    # open QL2 website 
    browser.get('http://live.ql2.com') 

代码
csvList = browser.find_elements_by_class_name("csv") 
    for l in csvlist: 
      if 'error' not in l.text and 'after' not in l.text: 
       l.send_keys(Keys.CONTROL +'t') 

每个元件L被表示为如下:

<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="9003fc6a-d8be-472b-bced-94fffdb5fdbe", element="27e1638a-0e37-411d-8d30-896c15711b49")> 

问题

为什么我不能够打开一个新标签。有什么遗漏吗?

回答

1

这个问题似乎是,你只是做一个新的选项卡,而不是打开新标签中的链接。

尝试使用ActionChains

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

# create browser as detailed in OP's setup 
key_code = Keys.CONTROL 
csvList = browser.find_elements_by_class_name("csv") 
for l in csvlist: 
    if 'error' not in l.text and 'after' not in l.text: 
     actions = webdriver.ActionChains(browser) 
     # Holds down the key specified in key_code 
     actions.key_down(key_code) 
     # Clicks the link 
     actions.click(l) 
     # Releases down the key specified in key_code 
     actions.key_up(key_code) 
     # Performs the actions specified in the ActionChain 
     actions.perform() 
+0

我已经试过了。对不起,在我的问题中不清楚 –

+0

你是否在for循环中执行ActionChains?此外,我只是想检查控制+点击为您打开一个新标签。使用OSX,需要Keys.COMMAND,而不是Keys.CONTROL。 – Brydenr

+0

我在for循环中使用它,因为我想忽略包含单词“error”和“after”之后的CSV文件。我使用Ubuntu我应该使用控制? –