Selenium: ‘function’对象没有属性’click’

huangapple go评论57阅读模式
英文:

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:

&lt;button class=&quot;Component-button-0-2-65 Component-button-d1-0-2-68&quot;&gt;and 5 more&lt;/button&gt;

My code is here:

button = EC.element_to_be_clickable((By.XPATH,&#39;.//button[contains(@class,&quot;Component-button-d&quot;)]&#39;))
    if button:
        print(&quot;TRUE&quot;)
        button.click()

My output is:

TRUE
Traceback (most recent call last):
  File &quot;&quot;, line 47, in &lt;module&gt;
    button.click()
AttributeError: &#39;function&#39; object has no attribute &#39;click&#39;

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

以下是翻译好的部分:

  1. "The element search is always done through the driver object which is the WebDriver instance in its core form:" 翻译为:"元素搜索始终通过 driver 对象进行,它是 WebDriver 的核心实例:"

  2. "driver.find_element(By.XPATH, "element_xpath")" 翻译为:"driver.find_element(By.XPATH, "element_xpath")"

  3. "But when you apply WebDriverWait, the wait configurations are applied on the driver object." 翻译为:"但是,当您使用 WebDriverWait 时,等待配置将应用于 driver 对象上。"

  4. "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")))"

  5. "So even during WebDriverWait the driver object needs to be present along with the expected_conditions and the locator strategy." 翻译为:"因此,即使在 WebDriverWait 过程中,driver 对象也需要与 expected_conditionslocator strategy 一起存在。"

  6. "This usecase" 翻译为:"这个用例"

  7. "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")]'))"

  8. "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." 翻译为:"button 对象引用了一个垃圾值,但在探测时返回 true 并打印 TRUE,但无法执行点击操作,因为 button 不是 WebElement 类型。"

  9. "Solution" 翻译为:"解决方案"

  10. "Given the HTML:" 翻译为:"给定的HTML:"

  11. "<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>"

  12. "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:"

  13. "Using XPATH with classname:" 翻译为:"使用 XPATHclassname:"

  14. "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()"

  15. "Using XPATH with classname and innerText:" 翻译为:"使用 XPATHclassnameinnerText:"

  16. "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()"

  17. "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, &quot;element_xpath&quot;)

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, &quot;element_xpath&quot;)))	

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,&#39;.//button[contains(@class,&quot;Component-button-d&quot;)]&#39;))

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:

&lt;button class=&quot;Component-button-0-2-65 Component-button-d1-0-2-68&quot;&gt;and 5 more&lt;/button&gt;

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, &quot;//button[contains(@class,&#39;Component-button-d&#39;)]&quot;))).click()
    
  • Using XPATH with classname and innerText:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//button[contains(@class,&#39;Component-button-d&#39;) and text()=&#39;and 5 more&#39;]&quot;))).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, &#39;//div[@class=&quot;input-group&quot;]&#39;)))
element.click()

Selenium documentation can be found here.

huangapple
  • 本文由 发表于 2023年2月10日 03:10:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定