2014-02-19 4 views
1

샘플 코드를 작성했지만 작동하지 않습니다. 또한 2 탭에 대해서만 1 개의 창 핸들이 있다는 것을 관찰했습니다. 상위 탭으로 다시 전환하는 방법?selenium webdriver를 사용하여 상위 탭으로 다시 전환

driver = webdriver.Firefox() 
driver.set_page_load_timeout(60) 
driver.implicitly_wait(15) 
driver.get("https://www.google.co.in") 
oldtab = driver.current_window_handle 
print oldtab 
print driver.title 
body = driver.find_element_by_tag_name("body") 
print 'new tab opened' 
driver.get("http://gmail.com/") 
print driver.title 
print 'back to old tab' 
driver.switch_to_window(oldtab) 
print driver.title 
for handle in driver.window_handles: 
    print "Handle = ",handle 

답변

0

다음 솔루션이 나를 위해 작동합니다.

ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform() 
time.sleep(5)  
ActionChains(driver).key_down(Keys.CONTROL).send_keys(Keys.NUMPAD1).key_up(Keys.CONTROL).perform() 
1

당신이 달성 할 수있는 또 다른 방법이있다 - 브라우저의 열린 두 인스턴스는 driver1driver2 말을하고 브라우저 인스턴스에서 해당 URL을 열고에 대한 작업을 수행 - 당신은 탭을 전환 할 필요가

driver1 = webdriver.Firefox() 
driver1.get("https://www.google.co.in") 
//perform actions for page https://www.google.co.in 


driver2 = webdriver.Firefox() 
driver2.get("http://gmail.com/") 
//perform actions for page http://gmail.com/ 
5

을 상위 탭에 handle을 전환하기 전에 Keys을 사용하십시오.

from selenium.webdriver.common.keys import Keys 

driver = webdriver.Firefox() 
driver.set_page_load_timeout(60) 
driver.implicitly_wait(15) 

# First Tab 
driver.get("https://www.google.co.in") 
oldtab = driver.current_window_handle 
print driver.title 
time.sleep(3) 

# Second Tab 
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + "t") 
driver.get("http://gmail.com/") 
newtab = driver.current_window_handle 
print driver.title 
time.sleep(3) 

# Go back to First Tab 
driver.find_element_by_tag_name("body").send_keys(Keys.ALT + Keys.NUMPAD1) 
driver.switch_to_window(oldtab) 
print driver.title 
time.sleep(3) 

# Go to Second Tab again 
driver.find_element_by_tag_name("body").send_keys(Keys.ALT + Keys.NUMPAD2) 
driver.switch_to_window(newtab) 
print driver.title 
time.sleep(3) 
+0

'Google Gmail Gmail Gmail'을 인쇄하지만 ' Gmail Google Gmail'이어야합니다. – SIslam

0

창 (파이어 폭스)

편집 Dhiraj의 코드는 윈도우

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

driver = webdriver.Firefox() 
driver.set_page_load_timeout(60) 
driver.implicitly_wait(15) 

# First Tab 
driver.get("https://www.google.co.in") 
oldtab = driver.current_window_handle 
print driver.title 
time.sleep(3) 

# Second Tab 
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + "t") 
driver.get("http://gmail.com/") 
newtab = driver.current_window_handle 
print driver.title 
time.sleep(3) 

# Go back to First Tab 
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + Keys.PAGE_UP) 
driver.switch_to_window(oldtab) 
print driver.title 
time.sleep(3) 

# Go to Second Tab again 
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + Keys.PAGE_UP) 
driver.switch_to_window(newtab) 
print driver.title 
time.sleep(3) 

driver.close() 

그것은

을 인쇄에 FF41에서 작업 할 수있는 또 다른 완전한 버전 (NO 조각)
Google 
Gmail 
Google 
Gmail 
관련 문제