簡體   English   中英

右鍵單擊href並選擇“保存鏈接”,如Python Selenium

[英]Right click on an href and choose save link as in Python Selenium

我想單擊Exported_Systems href,然后在下拉選項中選擇“將鏈接另存為”。 我該怎么辦?

 <a href="/core/cache/0WDb_ukdMUOA7qoW9lt1cgnee0I=/Exported_Systems.csv" target="_blank">Exported_Systems.csv</a> 

這可能不是完美的解決方案,但它將模擬工作。 您可以使用動作鏈來打開右鍵單擊菜單。

from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
driver.get(link)
elem = driver.find_element_by_css_selector('a[target="_blank"]')
actionChain = ActionChains(driver)
actionChain.context_click(elem).perform()

我已經使用“目標”屬性來選擇標簽。 但是現在的問題是,對該菜單的訪問超出了硒的范圍。 因此,在這里,我使用pyautogui模擬了4個向下箭頭鍵和回車按鈕。(4個向下箭頭鍵作為“保存鏈接”選項在每個錨定標簽中排第4)

import pyautogui
pyautogui.typewrite(['down','down','down','down','enter'])

希望這可以幫助。

另一種方法是使用配置文件以防止下載對話框:

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/tmp')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')

driver= webdriver.Firefox(profile)
driver.get("yourWebSite")

driver.find_element_by_xpath('//a[@href][text()[contains(., 'Exported_Systems')]]').click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM