本文介绍了在selenium webdriver中切换到web对话框:Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在selenium web驱动程序(Internet Explorer)下处理一个Web对话框。我正在使用Python



在我的应用程序中,当我点击Icon时,会打开一个包含一些文本框(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(r​​eport_cutoff_date)。sendkeys(10/31/2010)

这是我得到的错误

 回溯(最近一次呼叫最后):
文件C:\Users\vthaduri\workspace\LDC\test.py,第14行,< module>
driver.find_element_by_name(report_cutoff_date)。sendkeys(10/31/2010)
文件C:\Python27 \lib \site-packages \selenium-2.21.2- py2.7.egg\selenium\webdriver\remote\webdriver.py,第282行,在find_element_by_name
返回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,第651行,在find_element $ b中$ b {'使用':by,'value':value})['value']
文件C:\Python27 \lib \site-packages \selenium-2.21.2-py2。 7.egg\selenium\webdriver\remote\webdriver.py,第153行,执行
self.error_handler.check_response(response)
文件C:\Python27 \ lib \ site-packages \selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\errorhandler.py,第147行,在check_response
中引发exception_class(消息,屏幕, stacktrace)
selenium.common.exceptions.NoSuchElementException:消息:u'无法找到名称为== report_cutoff_date'的元素'

为了您的信息,我们的元素具有相同的名称和截止日期。



有人可以帮助我。

  parent_h = browser.current_window_handle 
#点击打开新窗口的链接
handles = browser.window_handles#在弹出窗口关闭之前
handles.remove(parent_h)
browser.switch_to_window(handles.pop( ))
#弹出窗口中的东西
#弹出窗口关闭
browser.switch_to_window(parent_h)
#并且你回来了


I want to handle a web dialog box under selenium web driver (Internet Explorer) . I am using Python

In my application when I click on Icon, a web dialog box opens which contains some text boxes (Webelements) and I need to click on save button after entering some text. The problem is I dont know whether the focus got switched to the web dialog box or not. Here is my code

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

Here is the error I am getting

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'

For your information the webelement is present with same name and cutoff date.

Can some one help me on this.

解决方案

Try this:

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

这篇关于在selenium webdriver中切换到web对话框:Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 03:49