Selenium Python中的窗口切换错误
我有一个要求,我需要执行一些下拉操作,从子窗口的下拉列表中选择一个值,该值在for循环中从父窗口重定向。也就是说,父窗口中有多行&我需要遍历所有行,单击超链接并从下拉列表中选择相同的值。但当我只切换窗口时,我总是面临错误 需要指出的是,子窗口在同一位置打开,即子窗口不是弹出窗口,两个窗口中的元素都位于具有相同名称和id的iFrame下 我已经在事件列表中存储了需要迭代的所有事件值 我有下面的代码片段Selenium Python中的窗口切换错误,python,selenium,firefox,automation,browser-automation,Python,Selenium,Firefox,Automation,Browser Automation,我有一个要求,我需要执行一些下拉操作,从子窗口的下拉列表中选择一个值,该值在for循环中从父窗口重定向。也就是说,父窗口中有多行&我需要遍历所有行,单击超链接并从下拉列表中选择相同的值。但当我只切换窗口时,我总是面临错误 需要指出的是,子窗口在同一位置打开,即子窗口不是弹出窗口,两个窗口中的元素都位于具有相同名称和id的iFrame下 我已经在事件列表中存储了需要迭代的所有事件值 我有下面的代码片段 import time from selenium import webdriver brows
import time
from selenium import webdriver
browser = webdriver.Firefox(executable_path=r'M:\Parvej\Softwares\Python-Selenium\geckodriver.exe')
browser.get('<URL ADDRESS>')
emailElem = browser.find_element_by_id('i0116')
emailElem.send_keys('user@gmail.com')
linkElem = browser.find_element_by_id('idSIButton9')
linkElem.click()
time.sleep(10);
list = browser.find_element_by_tag_name('iframe')
iframe1 = browser.find_element_by_id("gsft_main")
browser.switch_to.frame(iframe1)
time.sleep(10)
incident_list = []
for tr_incidents in browser.find_elements_by_partial_link_text('INC'):
incidents = tr_incidents.text
incident_list.append(incidents)
print(incident_list)
for inc in incident_list:
str1=browser.title
print(str1)
#get the window handles using window_handles( ) method
window_before = browser.window_handles[0]
print(window_before)
#since the pop-up is now in an iframe
#DID NOT SWITCH TO IFRAME BCOZ I HAVE ALREADY SWITCHED TO IT ABOVE
#browser.switch_to.frame(browser.find_element_by_id('gsft_main'))
inc_elem = browser.find_element_by_link_text(inc)
inc_elem.click()
#get the window handle after a new window has opened
window_after = browser.window_handles[1]
#switch on to new child window
browser.switch_to.window(window_after)
str2=browser.title
print(str2)
print(window_after)
#assert that main window and child window title don't match
self.assertNotEqual(str1,str2)
print('This window has a different title')
#switch back to original window
browser.switch_to.window(window_before)
#assert that the title now match
self.assertEqual(str1,browser.title)
print('Returned to parent window. Title now match')
#browser.back()
但是,每次这样做时,我都会出现错误:
window\u after=浏览器。window\u句柄[1]
索引器:列表索引超出范围
请帮我解决这个问题
谢谢和问候