英文:
Selenium: 'function' object has no attribute 'click'
问题
我在使用Selenium尝试点击按钮时遇到了这个问题。按钮的HTML如下所示:
<button class="Component-button-0-2-65 Component-button-d1-0-2-68">and 5 more</button>
我的代码如下:
button = EC.element_to_be_clickable((By.XPATH, './/button[contains(@class,"Component-button-d")]'))
if button:
print("TRUE")
button.click()
我的输出是:
TRUE
Traceback (most recent call last):
File "", line 47, in
button.click()
AttributeError: 'function' object has no attribute 'click'
我困惑的是,Selenium找到了button
元素(执行print(True)
语句),但然后click()
方法返回了属性错误。
我正在从此页面提取数据:https://religiondatabase.org/browse/regions
我能够提取页面上需要的所有信息,因此导致点击操作的代码是有效的。
我原本期望该元素是可点击的。我不确定如何排除属性错误(函数对象没有click
属性)。因为如果我将XPath粘贴到网页上,它会突出显示正确的元素。
英文:
I am running into this issue when trying to click a button using selenium. The button html reads as below:
<button class="Component-button-0-2-65 Component-button-d1-0-2-68">and 5 more</button>
My code is here:
button = EC.element_to_be_clickable((By.XPATH,'.//button[contains(@class,"Component-button-d")]'))
if button:
print("TRUE")
button.click()
My output is:
TRUE
Traceback (most recent call last):
File "", line 47, in <module>
button.click()
AttributeError: 'function' object has no attribute 'click'
I am stumped as to why 'button' element is found by selenium (print(True)
statement is executed) but then the click()
method returns an attribute error.
This is the page I am scraping data from: https://religiondatabase.org/browse/regions
I am able to extract all the information I need on the page, so the code leading up to the click is working.
I was expecting the item to be clickable. I'm not sure how to troubleshoot the attribute error (function object has no attribute click). Because it I paste the xpath into the webpage, it highlights the correct element.
答案1
得分: 1
以下是翻译好的部分:
-
"The element search is always done through the
driver
object which is the WebDriver instance in its core form:" 翻译为:"元素搜索始终通过driver
对象进行,它是 WebDriver 的核心实例:" -
"driver.find_element(By.XPATH, "element_xpath")" 翻译为:"driver.find_element(By.XPATH, "element_xpath")"
-
"But when you apply WebDriverWait, the
wait
configurations are applied on thedriver
object." 翻译为:"但是,当您使用 WebDriverWait 时,等待配置将应用于driver
对象上。" -
"WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "element_xpath")))" 翻译为:"WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "element_xpath")))"
-
"So even during WebDriverWait the
driver
object needs to be present along with the expected_conditions and the locator strategy." 翻译为:"因此,即使在 WebDriverWait 过程中,driver
对象也需要与 expected_conditions 和 locator strategy 一起存在。" -
"This usecase" 翻译为:"这个用例"
-
"button = EC.element_to_be_clickable((By.XPATH,'.//button[contains(@class,"Component-button-d")]'))" 翻译为:"button = EC.element_to_be_clickable((By.XPATH,'.//button[contains(@class,"Component-button-d")]'))"
-
"button object references to a junk value but on probing returns
true
and prints TRUE but the click can't be performed asbutton
is not of WebElement type." 翻译为:"button 对象引用了一个垃圾值,但在探测时返回true
并打印 TRUE,但无法执行点击操作,因为button
不是 WebElement 类型。" -
"Solution" 翻译为:"解决方案"
-
"Given the HTML:" 翻译为:"给定的HTML:"
-
"<button class="Component-button-0-2-65 Component-button-d1-0-2-68">and 5 more</button>" 翻译为:"<button class="Component-button-0-2-65 Component-button-d1-0-2-68">and 5 more</button>"
-
"To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:" 翻译为:"要点击元素,您需要使用 WebDriverWait 来诱导 element_to_be_clickable(),并可以使用以下任何一种 locator strategies:"
-
"Using XPATH with classname:" 翻译为:"使用 XPATH 和 classname:"
-
"WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "//button[contains(@class,'Component-button-d')]"))).click()" 翻译为:"WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "//button[contains(@class,'Component-button-d')]"))).click()"
-
"Using XPATH with classname and innerText:" 翻译为:"使用 XPATH 、 classname 和 innerText:"
-
"WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class,'Component-button-d') and text()='and 5 more']"))).click()" 翻译为:"WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class,'Component-button-d') and text()='and 5 more']"))).click()"
-
"Note: You have to add the following imports :" 翻译为:"注意:您必须添加以下导入:"
英文:
The element search is always done through the driver
object which is the WebDriver instance in it's core form:
driver.find_element(By.XPATH, "element_xpath")
But when you apply WebDriverWait, the wait
configurations are applied on the driver
object.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "element_xpath")))
So even during WebDriverWait the driver
object needs to be present along with the expected_conditions and the locator strategy.
This usecase
This line of code:
button = EC.element_to_be_clickable((By.XPATH,'.//button[contains(@class,"Component-button-d")]'))
button object references to a junk value but on probing returns true
and prints TRUE but the click can't be performed as button
is not of WebElement type.
Solution
Given the HTML:
<button class="Component-button-0-2-65 Component-button-d1-0-2-68">and 5 more</button>
To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
-
Using XPATH with classname:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "//button[contains(@class,'Component-button-d')]"))).click()
-
Using XPATH with classname and innerText:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class,'Component-button-d') and text()='and 5 more']"))).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
答案2
得分: 0
你的代码有错误。以下是如何使用预期条件(以及WebDriverWait)的示例:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
## [...定义你的驱动程序等等]
wait = WebDriverWait(driver, 25)
## [打开一个URL等等]
element = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="input-group"]')))
element.click()
Selenium文档可以在这里找到。
英文:
Your code is incorrect. You can find below an example of how you can use Expected Conditions (along with WebdriverWait):
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
## [...define your driver here, other imports etc]
wait = WebDriverWait(driver, 25)
##[open a url, etc]
element = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="input-group"]')))
element.click()
Selenium documentation can be found here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论