0

I am trying to automate some test case using Python Selenium Webdriver. I click on a button which opens up a new window/iframe/popup. Which needs some data to be be added in text box and choose some from the drop box. I have two questions,

  1. How would I find if its a window/iframe/popup from the firepath in Mozilla?
  2. How to get the handle?

This is the xpath and content I see in firepath when I select the new window/iframe/popup(image attached), html/body/div[11]enter image description here

I have the code here, zenoss_url = "http://xxxxx/" xpath_uname = "html/body/form/div/div/input[1]" xpath_pwd = "html/body/form/div/div/input[2]" xpath_submitButton = "html/body/form/div/div/input[3]" xpath_eventsTab = "html/body/div[7]/div/div/div/div[2]/ul/li[2]/div/div/a" driver = webdriver.Firefox() driver.get(zenoss_url) handle = driver.find_element_by_xpath(xpath_uname) handle.send_keys(zenoss_uname) handle = driver.find_element_by_xpath(xpath_pwd) handle.send_keys(zenoss_pwd) driver.find_element_by_xpath(xpath_submitButton).submit() driver.implicitly_wait(10) driver.find_element_by_xpath("html/body/div[7]/div/div/div/div[2]/ul/li[2]/div/div/a").click() driver.find_element_by_xpath("html/body/div[3]/div/div/div/div[1]/div/div/div[1]/div/div/div[7]/em/button").click() driver.implicitly_wait(5) window = driver.switch_to_window(?????)

I am not sure what "name" i should replace with ?????

Appreciate any help.

ASANT
  • 411
  • 1
  • 6
  • 18

1 Answers1

0

Your path of html/body/div[11] is only the xpath from the scope of the frame. When navigating to frames and subframes there are a few approaches you can take; from looking for the index of the frame to looking for the name of the frame. If you use firepath to inspect an element inside the frame, it will only give you the xpath for the element the scope of that frame. Frames work like boxes inside one another; in order to get to the second box, you have to open the first box and look inside.

//In C# 
driver.switchTo().Frame("FirstFrameFound"); 
//To close the box again and get back to the overall main box. 
driver.switchTo().defaultContent(); 

If your 2nd frame is a child of the first frame, you have to dive into the first, and then switch to the 2nd to interact with elements inside.

//In C# 
//Switch to the first frame 
driver.switchTo().Frame("FirstFrameFound"); 
//Switch to the second frame 
driver.SwitchTo().Frame("SecondFrame"); 
//Now look for your element that you wish to interact with. 
driver.FindElement(By.XPath("//html/body/div[11]")); 
//To close the box again and get back to the overall main box. 
driver.switchTo().defaultContent(); 

Please look into this post here for more information.

How to navigate a subframe inside a frameset using Selenium WebDriver?

If you are switching to a window there are multiple approaches as well. My preferred approach is this method:

//When dealing with new window and the parent. 
//Switch to the last window that opened
driver.SwitchTo().Window(driver.WindowHandles.Last()); 
//Perform your actions
driver.Close(); 
//Switch back to the first window 
driver.SwitchTo().Window(driver.WindowHandles.First()); 
Community
  • 1
  • 1
Ben
  • 667
  • 6
  • 13
  • Note that `driver.WindowHandles` are not in order - `driver.WindowHandles.Last()` might not be the last window opened. – Faiz Feb 21 '14 at 03:03