[英]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.我正在尝试自动下载 excel 文件。 This is my first time using selenium and i don't normally code in Python so maybe a bit of a basic question.
这是我第一次使用 selenium 并且我通常不会在 Python 中编码,所以可能是一个基本问题。
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.我已经厌倦了 xpath 和完整的 xpath 都没有工作。
I am getting the following error.我收到以下错误。
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (213, 17).
selenium.common.exceptions.ElementClickInterceptedException:消息:元素单击被拦截:元素在点 (213、17) 处不可单击。 Other element would receive the click: ...
其他元素会收到点击: ...
The Inspector -> Element as in code. 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>
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试试下面的 JS 脚本示例
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)); 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). ElementClickInterceptedException是一个异常,当某个其他元素出现在另一个元素前面或某事会阻止真正的人工点击时(例如,当您需要向下滚动时)。 There are few solutions you can try.
您可以尝试的解决方案很少。
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
You can check if there is a need to scroll down before clicking.您可以在单击之前检查是否需要向下滚动。
driver.execute_script("window.scrollTo(0, Y)") # Y is the height
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 :要单击带有文本作为下载目录的
<input>
元素,您必须为element_to_be_clickable()
诱导WebDriverWait ,并且您可以使用以下任一定位器策略:
Using CSS_SELECTOR
and submit()
:使用
CSS_SELECTOR
和submit()
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.downloadBtn>input[value='Download Catalogues']"))).submit()
Using XPATH
and click()
:使用
XPATH
和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.然后在 CatDownloadBtn 之前,我使用 control + home 到达页面顶部。
#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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.