Python&Selenium:发生Selenium异常时代码不执行

2024-09-27 21:31:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个运行selenium webdriver的Python脚本,该脚本按以下步骤执行:

1)执行运行x次的for循环 2) 在主for循环中,selenium web驱动程序使用xpath查找页面上的按钮 3) 对于selenium找到的每个按钮,嵌套的For循环会单击每个按钮 4) 单击一个按钮后,弹出窗口打开,该窗口重定向弹出窗口中的随机网站 5) 在第二个弹出窗口中找到selenium,然后单击主弹出按钮,返回主窗口

这段代码在执行时运行良好,但是在发生selenium异常时会出现问题。在

1)如果弹出窗口有空白页,则发生selenium异常,但为该异常编写的代码没有执行 2) 如果弹出窗口在超时后由主网站关闭(不是由selenium webdriver关闭),那么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循环:

^{pr2}$

Tags: todivforbydriverseleniumfindwindow
2条回答

两件事:

1)在except中,您正在使用driver.close(),然后尝试将已关闭的driver与{}一起使用,因此出现错误:

selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed from unknown error: web view not found

2)你应该把tryexcept再加上一行具体的代码,我觉得太宽泛了。。。在

为了获得最佳实践,您不应该使用这样的结构化XPath:

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]")

它一定会断的。在

尝试使用css-selector或更特殊的XPath。在

希望这对你有帮助!在

我一定要先把窗户关上司机。关门(). 在

driver.switch_to.window(driver.window_handles[1]) 
driver.close() 
driver.switch_to.window(driver.window_handles[0])

相关问题 更多 >

    热门问题