簡體   English   中英

如何使用 Selenium 和 Python 單擊值為“下載目錄”的輸入元素

[英]How to click on the input element with value as Download Catalogues using Selenium and Python

我正在嘗試自動下載 excel 文件。 這是我第一次使用 selenium 並且我通常不會在 Python 中編碼,所以可能是一個基本問題。

我已經登錄並勾選了一個有效的復選框,但是倒數第二個步驟是點擊一個似乎是下載按鈕的。 我查看了堆棧溢出和谷歌我可以找到類似的問題,但我找不到解決我的問題的解決方案。 我通常使用

.find_element_by_xpath

這適用於其他所有內容,但不適用於下載按鈕。 我添加了一個等待以確保頁面已完全加載,但這並沒有讓它變得更容易。

#Downlod checked Catalogue
CatDownloadBtn = driver.find_element_by_xpath('/html/body/div[3]/form/div[2]/input[1]')
CatDownloadBtn.click()

我已經厭倦了 xpath 和完整的 xpath 都沒有工作。

我收到以下錯誤。

selenium.common.exceptions.ElementClickInterceptedException:消息:元素單擊被攔截:元素在點 (213、17) 處不可單擊。 其他元素會收到點擊: ...

在此處輸入圖像描述

Inspector -> Element 就像代碼一樣。

<div class="col-md-offset-0 col-md-10 downloadBtn">
        <input name="submit" type="submit" class="btn btn-default" value="Download Catalogues">
        <input name="submit" type="submit" class="btn btn-default" value="Download Attributes">
        <input name="submit" type="submit" class="btn btn-default" value="Download Enhanced Data">
</div>

這通常是因為您嘗試單擊的元素不可點擊,有多個“構建塊”來制作一個工作元素,此錯誤通常發生是因為您嘗試單擊錯誤的“塊”。

解決此問題的一種方法是嘗試單擊不同的元素,無論是向上還是向下,我認為在您的情況下,它應該進一步向上,因為“輸入”通常不可點擊。

由於我不知道網站是什么,我無法提供具體信息

表示由於目標元素以某種方式被遮擋而無法正確執行單擊。

試試下面的 JS 腳本示例

ele = driver.find_element_by_xpath("//input[@class='button']")
driver.execute_script("arguments[0].click();", element)

另外,如果不起作用,請嘗試等待

wait.until(ExpectedConditions.elementToBeClickable(element));

ElementClickInterceptedException是一個異常,當某個其他元素出現在另一個元素前面或某事會阻止真正的人工點擊時(例如,當您需要向下滾動時)。 您可以嘗試的解決方案很少。

解決方案 1(Javascript 執行器):

CatDownloadBtn = driver.find_element_by_xpath('/html/body/div[3]/form/div[2]/input[1]')
driver.execute_script('arguments[0].click()', CatDownloadBtn) # Performs a Javascript click

解決方案 2(確保元素不會被攔截):

您可以在單擊之前檢查是否需要向下滾動。

driver.execute_script("window.scrollTo(0, Y)") # Y is the height

額外的:

可以使用以下值按選擇按鈕:

CatDownloadBtn = driver.find_element_by_xpath('//input[@value="Download Catalogues"]')

要單擊帶有文本作為下載目錄<input>元素,您必須為element_to_be_clickable()誘導WebDriverWait ,並且您可以使用以下任一定位器策略

  • 使用CSS_SELECTORsubmit()

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.downloadBtn>input[value='Download Catalogues']"))).submit()
  • 使用XPATHclick()

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'downloadBtn')]/input[@value='Download Catalogues']"))).click()
  • 注意:您必須添加以下導入:

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

您好,感謝您的所有幫助和解決方案,非常感謝大家的時間。 所以我設法使用以下方法解決了這個問題。

from selenium.webdriver.common.keys import Keys

然后在 CatDownloadBtn 之前,我使用 control + home 到達頁面頂部。

#Go back to the top of the page
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)

所以我的完整片段看起來像這樣。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()

#Go back to the top of the page
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)

#Downlod checked Catalogue
CatDownloadBtn = driver.find_element_by_xpath('/html/body/div[3]/form/div[2]/input[1]')
CatDownloadBtn.click()

暫無
暫無

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

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