python selenium禁用os弹出窗口

时间:2016-11-10 15:37:44

标签: python selenium popup operating-system

我正在尝试使用selenium中的Python自动执行文件下载过程。到目前为止,我成功地编写了代码来实现它。但是只有一个小问题非常令人不安:每次我在Firefox启动程序时(我使用webdriver.Firefox()),总会有一个操作系统弹出窗口询问我allow the website to use 'Microsoft Office'并阻止整个程序。由于这是一个操作系统问题,我无法使用selenium进行交互...我也尝试了driver.switch_to_alert()方法,但它没有用。

你知道怎么解决吗?

非常感谢!!

2 个答案:

答案 0 :(得分:0)

try:
        WebDriverWait(driver, 40).until(EC.alert_is_present(),
                                      'Timed out waiting for PA creation ' +
                                      'confirmation popup to appear.')

        alert = driver.switch_to.alert()
        alert.accept()

答案 1 :(得分:-1)

这是我的整个代码,弹出窗口出现在这两行之间(登录后):

loginButtonElement.click()
BBElement=WebDriverWait(driver,50).until(lambda driver:driver.find_element_by_xpath(BBButton))

代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest
import os




class LoginTest(unittest.TestCase):
  def setUp(self):

    fp=webdriver.FirefoxProfile()

    fp.set_preference("browser.download.folderList",2)
    fp.set_preference("browser.download.manager.showWhenStarting",False)
    fp.set_preference("browser.download.dir", "D://doc")
    fp.set_preference("pdfjs.disabled", True)
    fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/pdf")
    fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")


    self.driver=webdriver.Firefox(firefox_profile=fp)
    self.driver.get("myurl")

  def test_Login(self):
    driver=self.driver

    emailFieldID="userNameInput"

    passFieldID="passwordInput"
    loginButtonID="submitButton"
    BBButton="(//a[contains(@href,'blackboard')])"
    coursebutton="(//a[contains(@href,'Course&id=_4572_1&url')])[1]"

    docbutton="(//a[contains(@href,'content_id=_29867_1')])"
    conbutton="(//a[contains(@href,'content_id=_29873_1')])"
    paperbutton="(//a[contains(@href,'/xid-26243_1')])"

    emailFieldElement=WebDriverWait(driver,20).until(lambda driver:driver.find_element_by_id(emailFieldID))

    passFieldElement=WebDriverWait(driver,20).until(lambda driver:driver.find_element_by_id(passFieldID))
    loginButtonElement=WebDriverWait(driver,20).until(lambda driver:driver.find_element_by_id(loginButtonID))

    emailFieldElement.clear()
    emailFieldElement.send_keys("username")
    passFieldElement.clear()    
    passFieldElement.send_keys("password")
    loginButtonElement.click()
    BBElement=WebDriverWait(driver,50).until(lambda driver:driver.find_element_by_xpath(BBButton))
    BBElement.click()