【问题标题】:Getting out of popup form after downloading PDF selenium python chromedriver下载 PDF selenium python chromedriver 后退出弹出表单
【发布时间】:2018-12-03 18:38:30
【问题描述】:

我最近在从弹出表单下载 PDF 文件方面获得了帮助,但现在我想退出表单,但由于某种原因,我在下载 PDF 后无法执行任何操作。

我试过ActionChains(driver).send_keys(Keys.ESCAPE).perform() 并没有退出表单。我也尝试过driver.back(),但这会回到一页,我想留在当前页面继续下载文件。

这是目前为止的代码:

options = webdriver.ChromeOptions()
prefs = {"download.default_directory":"C:/Users/gille/Documents/SJ Webscraping/data","plugins.plugins_list":[{"enabled":False,"name":"Chrome PDF Viewer"}]}
options.add_experimental_option("prefs",prefs)

driver = webdriver.Chrome(options=options)
driver.get("https://www.southtechhosting.com/SanJoseCity/CampaignDocsWebRetrieval/Search/SearchByElection.aspx")
driver.find_element_by_xpath('//*[@id="ctl00_DefaultContent_ASPxRoundPanel1_btnFindFilers_CD"]').click()
driver.find_element_by_xpath('//*[@id="ctl00_GridContent_gridFilers_DXCBtn0"]').click()
driver.find_elements_by_xpath('//td[@class="dxgvCommandColumn_Glass dxgv"]//img[@title="View Form"]')[0].click()

driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
a = driver.find_element_by_link_text("Click here")
ActionChains(driver).key_down(Keys.CONTROL).click(a).key_up(Keys.CONTROL).perform()

#insert exit sequence here

尝试退出以下弹出表单:

【问题讨论】:

  • 您可以使用预期条件from selenium.webdriver.support import expected_conditions as EC 来检查警报,切换到它们并接受它们
  • 我试图了解 expected_conditions 在这种情况下是如何工作的。我试过检查警报,但它不起作用。您能否提供一个代码示例,说明如何将 expected_conditions 应用于此问题?
  • 这只是一种检查方式,可能不是警报消息。if EC.alert_is_present():\alert=driver.switch_to.alert\alert.accept()
  • 获取WebDriverException: Message: chrome not reachable. --> alert = driver.switch_to.alert

标签: python selenium selenium-chromedriver


【解决方案1】:

我已经更新了我的答案here,但万一你错过了,

试试这个关闭弹出窗口:

driver.switch_to.default_content()
driver.find_element_by_xpath('//*[@id="ctl00_GenericPopupSizeable_InnerPopupControl_HCB-1"]/img').click()

解释:弹出的关闭按钮不在下载链接可见的iframe中,所以我们需要回到主要内容,然后点击弹出关闭按钮。

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2021-11-21
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多