我有一个网页,其中包含一些内容,而我拥有一个具有某些价值的Excel。我正在尝试在页面中查找元素(Excel值),如果找到它,则必须单击该另一循环到下一个元素。切换到main(Parent)Window时,一切都很好,直到第一行,我得到的异常是:
File "C:/Users/tmou/PycharmProjects/Python/WebScraping/ST.py", line 47, in <module>
if driver.find_elements_by_xpath(xpath):
File "C:\Users\tmou\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 410, in find_elements_by_xpath
return self.find_elements(by=By.XPATH, value=xpath)
File "C:\Users\tmou\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1007, in find_elements
'value': value})['value'] or []
File "C:\Users\tmou\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\tmou\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
但是直到循环(excel)完成,我的网页才关闭。
for c in range(sheet.ncols):
for r in range(sheet.nrows):
st = (sheet.cell_value(r, c))
#print(st)
xpath = "//input[@value='Analyze' and contains(@onclick,'" + st + "')]"
if driver.find_elements_by_xpath(xpath):
driver.find_element_by_xpath(xpath).click() # Here new window will open
time.sleep(2)
#Main_Window = driver.current_window_handle
handles = driver.window_handles
h = len(handles) # h=2
for handle in range(1, h):
# for handle in range(h):
driver.switch_to.window(handles[handle])
driver.find_element_by_xpath(
'/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
#time.sleep(2)
driver.close()
lst_window = driver.window_handles
h2 = len(lst_window) # h2=2
#for lst_handle in range(2, h2):
for lst_handle in range(1,h2):
driver.switch_to.window(lst_window[lst_handle])
xpath2 = "//*[@id='create_button']"
xpath3 = "//*[@id='update_button']"
if check_exists_by_xpath(xpath2):
driver.find_element_by_xpath(xpath2).click()
driver.close()
driver.switch_to.default_content()
elif check_exists_by_xpath(xpath3):
driver.find_element_by_xpath(xpath3).click()
driver.close()