2017-06-12 8 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.COMTROLの代わりにKeys.COMMANDが必要になります。 – Brydenr

+0

"error"という単語と "after"の後ろにあるCSVファイルを無視したいので、forループの中で使っています。私はubuntuを使用して私はコントロールを使用する必要がありますか? –

関連する問題