python selenium中自动下载文件的问题

python selenium中自动下载文件的问题

我有以下代码,可以很好地浏览 3 个页面并点击链接在 Firefox 中下载 .csv 文件。但使用配置文件首选项,无法自动下载。请帮忙。我在开始时设置了配置文件设置,它正确吗……?

from lib2to3.pgen2 import driver
from selenium import webdriver
import unittest
from webbrowser import browser
from selenium.webdriver.common.keys import Keys
import selenium
import time

class UnitTestExample(unittest.TestCase):

    def setUp(self):
        profile = webdriver.FirefoxProfile()
        profile.set_preference("browser.download.folderList", 2)
        profile.set_preference("browser.download.manager.showWhenStarting", False)
        profile.set_preference("browser.download.dir", "C:\Python34")
        profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")

        self.driver = webdriver.Firefox(firefox_profile=profile)  # note that this creates a class instance variable
        self.driver.implicitly_wait(30)  # when testing AJAX this would not be used
        self.base_url = "https://webtac.industrysoftware.automation.siemens.com/webpr/webpr.php?objtype=frames&g_userid=fddd&g_session_id=7311876"  # sets up another class instance variable
        self.verificationErrors = []

    def testWebSite1(self):  # a first test case there can be others
        driver = self.driver  # class instance variable used here
        driver.get(
            "https://webtac.industrysoftware.automation.siemens.com/webpr/webpr.php?objtype=frames&g_userid=a3rgcw&g_session_id=7311876")

        username = driver.find_element_by_id("username")
        password = driver.find_element_by_id("password")

        username.send_keys("username")
        password.send_keys("password")

        ###  driver.find_element_by_class_name("btn btn-primary").send_keys("\n")

        driver.find_element_by_xpath('//button[@type="submit"]').submit()
        time.sleep(30)
        # Call java fuction and web page loads
        driver.execute_script('javascript:parent.gotoSearch(\'advanced\')')
        # Add below dates in input fields
        time.sleep(10)

        driver.switch_to.frame(0)
        time.sleep(10)

        openf = driver.find_element_by_id("openedFrom_dateText")
        opent = driver.find_element_by_id("openedTo_dateText")
        openf.clear();
        openf.send_keys("01-Jan-2015")
        opent.clear();
        opent.send_keys("02-Jan-2015")
        time.sleep(20)
         # Print status
        print(driver.current_url)
        window_before = driver.window_handles[0]
        print(window_before)

        #Button click
        driver.find_element_by_xpath('//input[@type="button"]').click()
        time.sleep(10)

        window_after = driver.window_handles[1]
        driver.switch_to.window(window_after)
        print(driver.current_url)
        driver.find_element_by_link_text("[Comma-Delimited Text (CSV)]").click()
        print(window_after)
        time.sleep(10)
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)


if __name__ == "__main__":  # allows unittest to start by running this class file
    unittest.main()  # start the main method of unittest

相关内容