我正在执行浏览器自动化,但在某个时刻被阻止:有一刻,我要求浏览器单击一个按钮,然后打开一个新窗口。但有时互联网太慢,所以这个新窗口需要时间来加载。我想知道如何让 Selenium 等到这个新窗口完全加载。
这是我的代码:
driver.switch_to.default_content()
Button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'addPicturesBtn')))
Button.click()
newWindow = driver.window_handles
time.sleep(5)
newNewWindow = newWindow[1]
driver.switch_to.window(newNewWindow)
newButtonToLoad = driver.find_element_by_id('d')
newButtonToLoad.send_keys('pic.jpg')
uploadButton = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'uploadPics')))
uploadButton.click()
driver.switch_to.window(newWindow[0])
Run Code Online (Sandbox Code Playgroud)
我不时收到此错误:
newNewWindow = newWindow[1]
IndexError:列表索引超出范围
这让我觉得一个简单的 'time.sleep(5)' 不起作用。
所以,我的问题是,如何才能等到这个新窗口完全加载后再与之交互?
谢谢