<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] 前言 在定位元素的時候,經常會遇到各種異常,為什么會發生這些異常,遇到異常又該如何處理呢? <br /> ## 一、發生異常 1. 打開博客首頁,定位“新隨筆”元素,此元素id="blog\_nav\_newpost" 2. 為了故意讓它定位失敗,我在元素屬性后面加上xx 3. 運行失敗后如下圖所示,程序在查找元素的這一行發生了中斷,不會繼續執行click事件了 ![Snipaste_2020-09-14_17-36-50.png](http://i.loli.net/2020/09/14/lftJ5ih73Gs4BN9.png) <br /> ## 二、捕獲異常 1. 為了讓程序繼續執行,我們可以用try...except...捕獲異常。捕獲異常后可以打印出異常原因,這樣以便于分析異常原因 2. 從如下異常內容可以看出,發生異常原因是:NoSuchElementException selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"blog\_nav\_newpostxx"} 3. 從selenium.common.exceptions 導入 NoSuchElementException類 ![Snipaste_2020-09-14_17-40-44.png](http://i.loli.net/2020/09/14/iImMJHolgaVszxK.png) <br /> ## 三、參考代碼: ``` from selenium import webdriver from selenium.common.exceptions import NoSuchElementException driver = webdriver.Chrome() driver.get("https://www.cnblogs.com/zjut-cheng/") # 定位首頁"新隨筆" try: element = driver.find_element("id", "blog_nav_newpostxx") except NoSuchElementException as msg: print("查找元素異常 %s" %msg) # 點擊該元素 else: element.click() ``` <br /> ## 四、selenium常見異常 ``` 1.NoSuchElementException:沒有找到元素 2.NoSuchFrameException:沒有找到iframe 3.NoSuchWindowException:沒找到窗口句柄handle 4.NoSuchAttributeException:屬性錯誤 5.NoAlertPresentException:沒找到alert彈出框 6.lementNotVisibleException:元素不可見 7.ElementNotSelectableException:元素沒有被選中 8.TimeoutException:查找元素超時 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看