小编典典

切换到Selenium Webdriver中的Web对话框:Python

selenium

我要处理Selenium Web驱动程序(Internet Explorer)下的Web对话框。我正在使用Python

在我的应用程序中,单击“图标”时,将打开一个包含一些文本框(Webelements)的Web对话框,输入一些文本后,我需要单击“保存”按钮。问题是我不知道焦点是否切换到Web对话框。这是我的代码

driver.find_element_by_xpath("//img[contains(@src,'/images/btn_add.gif')]").click()
driver.switch_to_alert()
driver.find_element_by_name("report_cutoff_date").sendkeys("10/31/2010")

这是我得到的错误

Traceback (most recent call last):
File "C:\Users\vthaduri\workspace\LDC\test.py", line 14, in <module>
driver.find_element_by_name("report_cutoff_date").sendkeys("10/31/2010")
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 282, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 651, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 153, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to find element with name == report_cutoff_date'

供您参考,该网络元素的名称和截止日期相同。

有人可以帮我这个忙吗?


阅读 346

收藏
2020-06-26

共1个答案

小编典典

试试这个:

parent_h = browser.current_window_handle
# click on the link that opens a new window
handles = browser.window_handles # before the pop-up window closes
handles.remove(parent_h)
browser.switch_to_window(handles.pop())
# do stuff in the popup
# popup window closes
browser.switch_to_window(parent_h)
# and you're back
2020-06-26