如何使用 Selenium 和 Python 单击值为下载目录的输入元素

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

我正在尝试自动下载 excel 文件。这是我第一次使用 selenium,我通常不会在 Python 中编写代码,所以可能是一个基本问题。

我已经登录并勾选了一个有效的复选框,但倒数第二步是单击一个似乎是 .我查看了堆栈溢出和 google 我可以找到类似的问题,但我找不到解决我的问题的解决方案。我通常使用

.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: Message: element click intercepted: Element is not clickable at point (213, 17). Other element would receive the click: ...

检查器 -> 代码中的元素。

<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>

这通常是因为您尝试点击的元素不可点击,有多个 'building blocks' 来制作一个有效的元素,这个错误通常是因为您尝试点击错误的 'block'。

解决这个问题的一种方法是尝试点击不同的元素,向上或向下,我认为在你的情况下它应该更上一层楼,因为 'input' 通常是不可点击的。

因为我不知道这个网站是什么,所以不能给出具体的信息

表示点击无法正确执行,因为目标元素以某种方式被遮挡。

试试下面的 JS 脚本示例

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

此外,如果不起作用,请尝试等待

wait.until(ExpectedConditions.elementToBeClickable(元素));

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

方案二(保证元素不被拦截):

您可以在点击前检查是否需要向下滚动。

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

额外:

按钮可以通过选择,使用:

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

要单击文本为 下载目录<input> 元素,您必须诱导 for the element_to_be_clickable() and you can use either of the following :

  • 使用 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()