Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch to web dialog box in selenium webdriver: Python

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.

like image 714
vkrams Avatar asked Dec 26 '22 22:12

vkrams


1 Answers

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
like image 90
Sherwin Wu Avatar answered Jan 14 '23 08:01

Sherwin Wu