AttributeError:“list”对象没有“click”属性使用Selenium和Python单击元素时出错

2024-09-20 22:19:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要帮助这是我的脚本:

# Imports

from selenium import webdriver

url = "https://sjc.cloudsigma.com/ui/4.0/login"

d = webdriver.Chrome()
d.get(url)

escolhe = d.find_elements_by_xpath('//*[@id="trynow"]')
escolhe.click()

这就是html的外观:

<button id="trynow" class="btn g-recaptcha block full-width m-b dwse btn-warning" ng-class="{'btn-danger': instantAccess=='Error', 'btn-success': instantAccess=='Success', 'btn-warning': instantAccess=='Working', 'btn-warning': (instantAccess!='Working' &amp;&amp; instantAccess!='Success' &amp;&amp; instantAccess!='Error')}" data-ng-disabled="instantAccess=='Working' || instantAccess=='Success' || instantAccess=='Error'" analytics-on="click" analytics-event="Guest logged in" analytics-category="Guest logged in" analytics-label="Guest logged in" data-sitekey="6Lcf-2MUAAAAAKG8gJ-MTkwwwVw1XGshqh8mRq25" data-callback="onTryNow" data-size="invisible">
    <span name="btn-warning" class="default " style="font-size: 20px;">
        <i class="fa fa-thumbs-up"></i> Instant access
        <p style="font-size: 9px;font-style: italic;margin: 2px 0px;" class="ng-binding">No credit card is required</p>
    </span>
    <span name="btn-warning" class="working hide" disabled=""><i class="fa fa-spinner fa-spin"></i> Instant access...</span> <span name="btn-success" class="success hide" disabled=""><i class="fa fa-spinner fa-spin"></i> Entrar na sessão</span> <span name="btn-danger" class="error hide" disabled=""><i class="fa fa-exclamation-circle"></i> Erro</span> 
</button>

我需要帮助,因为每当我输入xpath时,都会出现以下错误:

AttributeError: 'list' object has no attribute 'click'


Tags: namedataerrornganalyticsclassfasuccess
3条回答

查找element而不是elements

from selenium import webdriver
import time

url = "https://sjc.cloudsigma.com/ui/4.0/login"

d = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe')
d.get(url)
time.sleep(5)  #Wait a little for page to load.
escolhe = d.find_element_by_xpath('//*[@id="trynow"]/span[1]')
escolhe.click()

必须写入元素而不是元素-->;即使只有一项,它也会将其视为列表。您必须这样修改:

from selenium import webdriver

url = "https://sjc.cloudsigma.com/ui/4.0/login"

d = webdriver.Chrome()
d.get(url)

escolhe = d.find_element_by_xpath('//*[@id="trynow"]')
escolhe.click()

此错误消息

AttributeError: 'list' object has no attribute 'click'

…表示您已尝试在列表元素上调用click()方法,其中在WebElement上调用asclick()


解决方案

要单击所需的元素,可以使用以下任意一种Locator Strategies

  • 使用css_selector

    driver.find_element_by_css_selector("button#trynow").click()
    
  • 使用xpath

    driver.find_element_by_xpath("//button[@id='trynow']").click()
    

理想情况下,要单击元素,需要为element_to_be_clickable()诱导WebDriverWait,并且可以使用以下Locator Strategies之一:

  • 使用CSS_SELECTOR

    driver.get('https://sjc.cloudsigma.com/ui/4.0/login')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#trynow"))).click()
    
  • 使用XPATH

    driver.get('https://sjc.cloudsigma.com/ui/4.0/login')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='trynow']"))).click()
    
  • 注意:您必须添加以下导入:

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

相关问题 更多 >

    热门问题