html tool

2015年5月11日星期一

selenium window switch

问题:多窗口切换,遍历窗口切换回最后的窗口内容
解决:
java版本
参考:http://blog.csdn.net/dongtianlaile/article/details/10176051

      //获取当前窗口 [popexizhi:保存切换前的handle 句柄]
      String winHandleBefore = driver.getWindowHandle();
      ... //other doing
      
 for (String handle : driver.getWindowHandles()) {
      System.out.println(handle);
      if (handle == winHandleBefore)
              continue; //旧窗口不操作
      else //只对新窗口操作 
       driver.switchTo().window(handle);
         
      }  

python版

add:http://www.cnblogs.com/Ralph-Wang/p/3393401.html

---------------------------------------
# 点击链接,打开新窗口
l = dr.find_element_by_xpath("//body/a")
l.click()
 
old_handle = dr.current_window_handle 
[popexizhi:保存旧的窗口句柄]
for handle in dr.window_handles:
    if old_handle != handle:
        new_handle = handle
        break

# 切换到新窗口中,点击新窗口中的按钮
dr.switch_to_window(new_handle)
btn = dr.find_element_by_xpath('//input[@id="b1"]')

没有评论:

发表评论