ah=drivercurrent_window_handleprintahallh = driverwindow_handles #获取所有的页面句柄nprintallh #打印出来的句柄是以列表的形式存在所以在访问的时候可以使用下标索引n#用for循环去找页面的第一种方法nnfor bh in allh if ah!=bh#若页面句柄不等于第一个句柄即相当于此时的bh已为新窗口句柄n
The code is trying to switch to a new window/tab and perform an action on the new window/tab. However, it is encountering a TimeoutException when waiting for an element to be located on the new window/tab.
To fix this issue, you can try increasing the timeout value in the WebDriverWait function. For example, you can change the line to:
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,'//*[@id="hot-key-SearchArea"]/div/div/div/div[2]/div/div[1]/div/div/span/div/input'))).send_keys('ZSJ_QY_SQ')
This will wait for a maximum of 20 seconds for the element to be located on the new window/tab before throwing a TimeoutException.
Alternatively, you can also check if the element is present on the new window/tab before performing any actions. You can use a try-except block to handle the TimeoutException and perform a different action or raise an error if the element is not found.
Here's an example:
try:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,'//*[@id="hot-key-SearchArea"]/div/div/div/div[2]/div/div[1]/div/div/span/div/input')))
element.send_keys('ZSJ_QY_SQ')
except TimeoutException:
print("Element not found on new window/tab")
This will catch the TimeoutException and print a message if the element is not found on the new window/tab
原文地址: https://www.cveoy.top/t/topic/h7y4 著作权归作者所有。请勿转载和采集!