python selenium disable os pop-ups

a 夏天 提交于 2019-12-13 05:45:04

问题


I am trying to automate the file downloading process with selenium in Python. Until now I succeeded in writing the code to realize it. But there is just a little problem which is pretty disturbing: Each time I launch the program in Firefox (I use webdriver.Firefox()), there is always an OS pop-up which asks me allow the website to use 'Microsoft Office' and blocks the whole program. Since it is an OS problem, I cannot interact with it using selenium...I also tried driver.switch_to_alert() method but it didn't work.

Do you know how to fix it?

Thank you very much!!


回答1:


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()



回答2:


Here is my entire code, the pop-up appears between those two lines (after login):

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

the code:

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()


来源:https://stackoverflow.com/questions/40531436/python-selenium-disable-os-pop-ups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!