硒:获取页面源时发出警报

时间:2018-09-11 10:35:16

标签: python selenium-webdriver

当尝试使用以下代码获取页面源代码时,我看到了Selenium的奇怪行为:

driver = webdriver.Firefox(firefox_profile=ff_profile, firefox_options=ff_options)
driver.get(url)
try:
    element = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "%s[%s='%s']" % (SectionTag, SectionAttrName, SectionAttrValue))))
    time.sleep(5)
    except TimeoutException as ex:
        driver.close()
    else:
        pageSource = driver.page_source
        try:
            driver.switch_to_alert().dismiss()
        except NoAlertPresentException:
            print ("no alert")
        driver.close()

这会导致错误:

    pageSource = driver.page_source
  File "/home/ura/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 532, in page_source
    return self.execute(Command.GET_PAGE_SOURCE)['value']
  File "/home/ura/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/home/ura/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message:

我尝试添加警报处理,但没有帮助。

有什么想法吗?

0 个答案:

没有答案