我有一个运行Selenium WebDriver的python脚本,它执行以下步骤:
1)执行运行x次的for循环
2)在主for循环中,Selenium Web驱动程序使用xpath查找页面上的按钮。
3)对于Selenium找到的每个按钮,嵌套的for循环单击每个按钮。
4)单击按钮后,弹出窗口将打开,该窗口将重定向弹出窗口中的随机网站。
5)此外,Selenium WebDriver在弹出窗口中找到其他按钮并单击该按钮,关闭弹出窗口并返回主窗口,单击主网站上的第二个按钮。
此代码在执行时运行良好,但在发生Selenium异常时出现问题。
1)如果弹出窗口有空白页,则会发生Selenium异常,但为该异常编写的代码没有执行。
2)如果超时后主网站关闭弹出窗口(Selenium WebDriver未关闭),则NoSuchWindowException会发生,但在此异常下不会执行
我多次尝试通过添加if-else条件来更改代码,但无法解决NoSuchWindowException异常
代码如下:
for _ in range(100):
print("main loop pass")
fb_buttons = driver.find_elements_by_xpath('//a[contains(@class,"pages_button profile_view")]')
for button in fb_buttons:
try:
time.sleep(10)
button.click()
driver.implicitly_wait(5)
driver.switch_to.window(driver.window_handles[1])
driver.execute_script("window.scrollTo(0, 2500)")
print("wiindow scrolled")
like_right = driver.find_elements_by_xpath(
"/html[1]/body[1]/div[1]/div[1]/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]")
like_left = driver.find_elements_by_xpath(
"/html/body/div[1]/div/div[2]/div/div[1]/div[1]/div[2]/div/div[2]/table/tbody/tr/td[1]/a[1]")
while like_right:
for right in like_right:
right.click()
break
while like_left:
for left in like_left:
left.click()
break
while like_post:
for like in like_post:
like.click()
break
time.sleep(5)
driver.close()
driver.implicitly_wait(5)
driver.switch_to.window(driver.window_handles[0])
print("clicks executed successfully")
continue
except StaleElementReferenceException as e:
driver.close()
driver.switch_to.window(driver.window_handles[0])
popunder = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[3]/p[2]/a")
if popunder is True:
popunder.click()
driver.implicitly_wait(5)
else:
continue
print("exception occured-element is not attached to the page document")
except ElementNotVisibleException as e:
driver.close()
driver.switch_to.window(driver.window_handles[0])
popunder = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[3]/p[2]/a")
if popunder is True:
popunder.click()
driver.implicitly_wait(5)
else:
continue
print("Exception occured - ElementNotVisibleException")
except WebDriverException as e:
driver.close()
driver.switch_to.window(driver.window_handles[0])
popunder = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[3]/p[2]/a")
if popunder is True:
popunder.click()
driver.implicitly_wait(5)
else:
continue
print("Exception occured - WebDriverException")
except NoSuchWindowException as e:
driver.switch_to.window(driver.window_handles[0])
popunder = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[3]/p[2]/a")
if popunder is True:
popunder.click()
driver.implicitly_wait(5)
else:
continue
print("Exception - NoSuchWindowException - Switched to main window")
else:
time.sleep(5)
refresh.click()
print("refreshed")
我试图通过python代码本身处理nosuchwindowexception,因为每当主网站关闭弹出窗口时,就会发生此异常,python脚本停止执行下一个for循环:
File "C:\Program Files (x86)\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
(Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 6.1.7601 SP1 x86_64)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/javed/PycharmProjects/clicks/test/fb-click-perfect-working.py", line 98, in <module>
driver.close()
File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 688, in close
self.execute(Command.CLOSE)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Program Files (x86)\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
(Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 6.1.7601 SP1 x86_64)
Process finished with exit code 1