【问题标题】:Switch to popup in python using selenium使用 selenium 在 python 中切换到弹出窗口
【发布时间】:2015-09-30 17:04:21
【问题描述】:

如何在下面的 selenium 程序中切换到弹出窗口。我已经查找了所有可能的解决方案,但无法理解它们。请帮忙!!

from selenium import webdriver
from splinter import Browser
from selenium.webdriver.common.keys import Keys
handle = []
driver = webdriver.Firefox()
driver.get("http://example.com/test.aspx")

driver.find_element_by_link_text("Site Actions").click()
driver.find_element_by_link_text('Edit Page').click()
select = driver.find_element_by_id('ctl00_PlaceHolderMain_ctl35_ctl00_SelectResult')
for option in select.find_elements_by_xpath('//*[@id="ctl00_PlaceHolderMain_ctl35_ctl00_SelectResult"]/option'):
            if option.text != 'Channel':
                option.select() # select() in earlier versions of webdriver

driver.find_element_by_id('ctl00_PlaceHolderMain_ctl35_ctl00_RemoveButton').click()

parent_h = driver.current_window_handle

#click that activates the popup.
checkIn = driver.find_element_by_id('qaCheckin_anchor').click()

# click on the link that opens a new window
handles = driver.window_handles # before the pop-up window closes
driver.remove(parent_h)
driver.switch_to_window(handles.pop())
driver.implicitly_wait(10) # seconds
driver.find_element_by_xpath('/html/body/form/div[3]/table/tbody/tr[4]/td/table/tbody/tr[3]/td[2]/input').click()
driver.find_element_by_name('btnClose2').click()
driver.close();
# do stuff in the popup
# popup window closes
driver.switch_to_window(parent_h)
# and you're back

driver.switch_to_default_content()

【问题讨论】:

  • @vks 谢谢。我已经尝试通过上述链接实施解决方案。但是,它对我不起作用。
  • 仍然无法正常工作。如果有帮助,我正在处理共享点页面。
  • 这是一个网页弹出窗口.. 带有复选框
  • 你要对popup进行什么操作?只需点击是/否?

标签: python selenium selenium-webdriver


【解决方案1】:

就浏览器而言,弹出窗口不是窗口,而是警报。所以,你应该使用以下:

driver.switch_to_alert()

【讨论】:

  • 不,它实际上是一个窗口。
  • 我也有同样的问题。
猜你喜欢
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多