Selenium Python FirefoxWebDriver處理開啟儲存對話方塊

來源:互聯網
上載者:User

標籤:mit   sky   http   web   manager   div   file   import   users   

 代碼如下(網上樣本):

#profile =  webdriver.FirefoxProfile(r"C:\Users\Skyyj\AppData\Roaming\Mozilla\Firefox\Profiles\1rzh6139.default")
profile = webdriver.FirefoxProfile()

##設定成0代表下載到瀏覽器預設下載路徑;設定成2則可以儲存到指定目錄
profile.set_preference("browser.download.folderList", 2)
#這裡設定與否不影響,沒有發現有什麼影響。
#profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", r"c:\Down")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
#這裡設定與否沒有發現有什麼影響
#profile.set_preference("browser.helperApps.alwaysAsk.force", False);
self.driver = webdriver.Firefox(profile)

 

 

自己寫的執行個體(Environment:Python3.6.5+Selenium+PyCharm+Firefox61+FirefoxDriver)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time,os

profile = webdriver.FirefoxProfile()

#save the file to directory=E:\sli\CIFS2
profile.set_preference("browser.download.dir", r"E:\sli\CIFS2")
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)

#download .zip file
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/zip")

#driver = webdriver.Firefox(firefox_profile=profile)
driver = webdriver.Firefox(profile)

driver.get("http://10.49.5.8:8080/RLink/login.jsp")
driver.maximize_window()
# -----------------------------------Login Steps----------------------------------------------
# input user name
driver.find_element_by_id("j_username").send_keys("1")
time.sleep(1)
# input password
driver.find_element_by_id("j_password").send_keys("1")
time.sleep(1)
# click "Log in" button
driver.find_element_by_id("buttonSubmit").click()
time.sleep(1)

# -----------------------------------Download Steps----------------------------------------------
# Highlight a zip file by xpath
driver.find_element_by_xpath("/html/body/div[6]/div[2]/div/div[7]/div/div/div[3]/div/div[4]/div/div[4]/div/div/div/div/div/ul/li[10]/div/table/tbody/tr[1]/td/div/div/div/table/tbody/tr/td/div/div/img").click()
time.sleep(2)
# click "Download" button
driver.find_element_by_xpath("/html/body/div[6]/div[2]/div/div[5]/div/table/tbody/tr/td[1]/table/tbody/tr/td[2]/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div").click()
time.sleep(2)

# -----------------------------------Logout Steps----------------------------------------------
# click "下拉框" of user menu
driver.find_element_by_xpath(‘/html/body/div[6]/div[2]/div/div[5]/div/table/tbody/tr/td[2]/table/tbody/tr/td/div/table/tbody/tr/td[2]/table/tbody/tr/td[2]/img‘).click()
time.sleep(1)
# click "Log Out"
driver.find_element_by_xpath("//div[text()=‘Log Out‘]").click()
time.sleep(1)
# click "Yes" on confirm dialog
driver.find_element_by_xpath(‘/html/body/div[9]/div/div/div/div[3]/div/div[2]/button[1]‘).click()
# click "No" on confirm dialog
#driver.find_element_by_xpath(‘/html/body/div[9]/div/div/div/div[3]/div/div[2]/button[2]‘).click()
time.sleep(1)
driver.quit()

 

 



 

Selenium Python FirefoxWebDriver處理開啟儲存對話方塊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.