简体   繁体   中英

move control to newly opened IE window in selenium webdriver (using python) :no window name , no window handle ,not a alert

Setup :python bindings for selenium 2.45.0 ,IEserver driver2.45.0(x86),python 2.7.9 ,window 7 64 bit

Issue : when i click on this redirect button href= https:www.work.test.co.in:1XXX9/TEST/servlet/MainServlet/home" target="_blank"

a new window opens , unable to click anything on new window as control(focus) remains on previous window (confirmed by closing the previous window).

Tried
1.no name , so cannot try

driver.switch_to_window("windowName")

2.tried to print the handle (so that i can use handle reference ) but i can see only one window handle . used following code

for handle in driver.window_handles:
  print "Handle arr = ",handle
  driver.switch_to_window(handle)

3.Question1 : why i am getting only one window handle handle , i can see two IE instances in task manager.

4.i tried using index - 0 ,1 etc.

driver.switch_to_window(driver.window_handles[-1])  

5.not sure of this thing though tried

driver.SwitchTo().Window(driver.WindowHandles.Last())

6.tried though i am sure that its not an alert window .

alert = driver.switch_to_alert() 

SCRIPT :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep  



driver = webdriver.Ie()
driver.get("https://my intranet site .aspx")
driver.implicitly_wait(2)
elem = driver.find_element_by_xpath("my xpath ")
elem.click()
driver.implicitly_wait(2)
elem = driver.find_element_by_xpath("//*[@id='tab1_2']/div[16]")
elem.click()

handle = driver.current_window_handle
print "Handle main  = ",handle

driver.implicitly_wait(5)
elem = driver.find_element_by_xpath("page link button")
elem.click()
sleep(5)
my tried scenarioes here

Suggestions will be highly appreciated Update - when new window opened directly through link URL , able to perform actions on it like clicking etc
So only issue is when I open it in continuation of first window through script.

Update : Main concern is why not getting second window handle even if task manager showing two instances of IE .

I don't know Python, but in Java I would do it in this way:

 // get handles to all opened windows before the click
 Set<String>  handlesBeforeTheClick = driver.getWindowHandles();
 // and now click on the link that opens a new window
 findElement( linkThatOpensNewWindow ).click();
 // then wait until a new window will be opened
 wait.until( ....condition ==> handlesBeforeClick.size() < driver.getWindowHandles().size(); .... )
 // then get a handle to a new window
 Set<String> handlesAfterClick = driver.getWindowHandles();
 handlesAfterClick.removeAll( handlesBeforeClick );
 String handleToNewWindow = handlesAfterClick.iterator().next();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM