简体   繁体   中英

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

I am trying to automate the downloading of an excel file. This is my first time using selenium and i don't normally code in Python so maybe a bit of a basic question.

I have got a login in and ticking a checkbox working, but the second to last step which is clicking a download button which seems to be a. I have looked around stack overflow and google i can find similar problems but i can not find a solution that fixes my problem. I am normally using

.find_element_by_xpath

Which works for everything else but not the download button. I have added a wait to make sure page is fully loaded but it does not make it any easier.

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

I have tired the xpath and full xpath neither worked.

I am getting the following error.

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (213, 17). Other element would receive the click: ...

在此处输入图像描述

The Inspector -> Element as in code.

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

This normally happens because the element your trying to click is not clickable, there are multiple 'building blocks' to make a working element, this error normally occurs because your trying to click the wrong 'block'.

A way to combat this is to try clicking different elements, either going up a level or further down, I think in your case it's supposed to be further up since 'input' normally isn't clickable.

Since I don't know what the website is, I can't give specifics

Indicates that a click could not be properly executed because the target element was obscured in some way.

Try JS script sample like below

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

Also if does not work try with waits

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

ElementClickInterceptedException is an exception which occurs when some other element comes in front of another or something which would prevent a real human click (eg when you need to scroll down). There are few solutions you can try.

Solution 1 (Javascript executer):

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

Solution 2 (Make sure element does not get intercepted):

You can check if there is a need to scroll down before clicking.

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

Extra:

Button can be selected by value using:

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

To click on the <input> element with text as Download Catalogues you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR and submit() :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.downloadBtn>input[value='Download Catalogues']"))).submit()
  • Using XPATH and click() :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'downloadBtn')]/input[@value='Download Catalogues']"))).click()
  • Note : You have to add the following imports:

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

Hello thanks for all your help and solutions really appreciate everyone's time. So i managed to solve this using the following.

from selenium.webdriver.common.keys import Keys

Then before CatDownloadBtn i use control + home to get to the top of the page.

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

So my full snippet would look like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM