简体   繁体   English

Selenium Python:无法点击按钮

[英]Selenium Python: Can't click the button

Hope you are all safe!希望你们都平安!

My issue is pretty common, and I've seen tons of similar questions, though things like "wait/explicitly wait/wait until clickable etc" didn't work for me.我的问题很常见,我见过很多类似的问题,尽管“等待/明确等待/等到可点击等”之类的东西对我不起作用。 I think I would like to ask for tailored solution for this problem:)我想我想为这个问题寻求量身定制的解决方案:)

I try to scrape some data from here http://kuap.ru/banks/8012/balances/en (I use Russian language version of the website, but I think source must be pretty the same)我尝试从这里http://kuap.ru/banks/8012/balances/en抓取一些数据(我使用网站的俄语版本,但我认为来源必须非常相同)

To do this, I have to choose appropriate data from drop-down list and then press button "Compare Data".为此,我必须从下拉列表中选择适当的数据,然后按“比较数据”按钮。 However, I can't click that button.但是,我无法单击该按钮。

The button按钮

My simple code:我的简单代码:

driver.find_element_by_css_selector('select[name = CurDate]').click()
select = Select(driver.find_element_by_css_selector('select[name = CurDate]'))
select.select_by_visible_text('01.01.2019')
driver.find_element_by_xpath('//*[@id="BalanceTable"]/thead/tr[1]/th[3]/input').click()

The last row doesn't actually click.最后一行实际上并没有点击。 I've seen that many times the problem is in the path, and I tried several CSS paths - nothing worked.我已经看到很多次问题出在路径上,我尝试了几个 CSS 路径 - 没有任何效果。 It looks like the same problem is here: when I try to get .text of the button it returns ' ' , whereas for other buttons it returns their actual text.看起来同样的问题在这里:当我尝试获取按钮的.text时,它返回' ' ,而对于其他按钮,它返回它们的实际文本。

My main hypothesis is that the problem may be in this mysterious "Shadow content" (See a snapshot) I can't view it in Safari.我的主要假设是问题可能出在这个神秘的“影子内容” (见快照)我无法在 Safari 中查看它。 I tried to access it via "...input//" (though it is a bit stupid, I know), but didn't manage to see it.我试图通过“...input//”访问它(虽然它有点愚蠢,我知道),但没能看到它。 I've read something about DOM etc... But it didn't really help (or I read badly)我读过一些关于 DOM 等的东西......但它并没有真正帮助(或者我读得很糟糕)

So, any useful hints will be of a very great value: Already spent several hours on this very basic stuff :)因此,任何有用的提示都将具有很大的价值:已经在这些非常基本的东西上花费了几个小时 :)

PS To make full disclosure - I'm super new to Python, trying to learn about data collection from the beginning. PS为了充分披露-我对Python非常陌生,试图从一开始就了解数据收集。

Try below xpath:试试下面的 xpath:

 wait = WebDriverWait(driver, 30)
 wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@class='btn btn-default']"))).click()

Note: please add below imports to your solution注意:请将以下导入添加到您的解决方案中

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

Working code:工作代码:

driver.get('http://kuap.ru/banks/8012/balances/en')
wait = WebDriverWait(driver, 10)
select = Select(driver.find_element_by_xpath("//select[@name='CurDate']"))
select.select_by_visible_text('01.01.2019')
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@class='btn btn-default']"))).click()

在此处输入图像描述

Another solution using java script::使用 java 脚本的另一个解决方案:

element=wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@class='btn btn-default']")))
driver.execute_script("arguments[0].click();", element)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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