簡體   English   中英

Selenium 帶<a>標簽的點擊按鈕 - 攔截異常</a>

[英]Selenium click button with <a> tag - intercepted exception

我正在嘗試使用 Selenium 在 Python 的網站上單擊一個按鈕。我想要的按鈕的 html 看起來像這樣:

<a onclick="exportGridExcel()"><span class="accordion-download"><i class="fa fa-file-excel-o" title="Download Excel" aria-hidden="true"></i></span></a>

html 的擴展版本是:

<div class="lang-switch-wrapper lang-switch-inverse-wrapper">
                            <div class="lang-switch">
                                <ul>
    <li class="dropdown">                        
        <a href="#" class="dropdown-toggle lang-lable" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
            <span class=" hidden-xs">English</span>
            <span class="hidden-lg hidden-md hidden-sm">EN</span>
            <img src="/etc/designs/wbrrdesign/clientlibs-wbrredsign/img/angle-down-gray.svg" alt="dropdown"></a>
        <ul class="dropdown-menu dropdown-item">
            <li><a href="https://espanol.enterprisesurveys.org/es/enterprisesurveys">Español</a></li>
        </ul>
    </li>
</ul>

                            </div>
                        </div>

然后在轉到我想要的按鈕組和按鈕之前還有一些其他的東西:

<div class="button-group">   
        <button onclick="onModifyQuery()" type="button" class="btn">Modify Query</button>
        <a onclick="exportGridExcel()"><span class="accordion-download"><i class="fa fa-file-excel-o" title="Download Excel" aria-hidden="true"></i></span></a>
        <a title="Undo to column removal" onclick="restoreColumn()" class="toggle-btn primary-light-blue-btn"><i class="fa fa-circle-o-notch" aria-hidden="true"></i></a>
    </div>

我的部分困惑是對這個 html 具有多個類和“i類”的經驗不足。

編輯

如果我嘗試類似的東西:

WebDriverWait(driver,300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick='exportGridExcel()']"))).click()

然后我得到錯誤:

ElementClickInterceptedException: element click intercepted: Element <a onclick="exportGridExcel()">...</a> is not clickable at point (772, 11). Other element would receive the click: <div class="lang-switch-wrapper lang-switch-inverse-wrapper">...</div>

問題是您的頁面會自動向上滾動,並且 excel 下載按鈕可能被包含語言選擇器的頂部橫幅隱藏了。 當 Selenium 嘗試點擊 excel 下載按鈕時,它找到了語言選擇器。

我建議您向上滾動頁面,直到整個 data.table 可見

你可以使用這樣的東西。 按HOME鍵go到頁面頂部

from selenium.webdriver.common.keys import Keys
...
...
element = WebDriverWait(driver,300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick='exportGridExcel()']")))
element.send_keys(Keys.CONTROL + Keys.HOME)
element.click()

理想情況下,根據您的代碼試驗點擊元素<a onclick="exportGridExcel()">elementToBeClickable()誘導WebDriverWait應該有效。

但是,似乎有一個后裔<span> 因此,您可以使用以下任一定位器策略

  • 使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='exportGridExcel'] > span.accordion-download"))).click()
  • 使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick, 'exportGridExcel')]/span[@class='accordion-download']"))).click()
  • 注意:您必須添加以下導入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

在另一種方法中,您還可以按如下方式使用JavascriptExecutor

  • 使用CSS_SELECTOR

     element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='exportGridExcel'] > span.accordion-download"))) driver.execute_script("arguments[0].click();", element)
  • 使用XPATH

     element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick, 'exportGridExcel')]/span[@class='accordion-download']"))) driver.execute_script("arguments[0].click();", element)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM