0

I need help this is my script:

# 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()

and this is what the html looks like:

<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>

I need help because whenever I put xpath this error:

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

1

Find element not 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()
Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38
0

You have to write element not elements --> Even if there is only one item it will treat it as a list. You have to modify like this:

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()
Matteo Bianchi
  • 434
  • 1
  • 4
  • 19
0

This error message...

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

...implies that you have tried to invoke click() method on a list element where as click() is invoked on a WebElement.


Solution

To click on the desired element you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("button#trynow").click()
    
  • Using xpath:

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

Ideally, 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 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()
    
  • Using 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()
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Your code returns a list of webelements. Webelements can be clicked on. You either need to select one webelement of the list by index, such as:

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

or use find_element_by_xpath to return the first element that matches your xpath locator:

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

I'd also suggest using find_element_by_id if you're using an id locator strategy:

escolhe = d.find_element_by_id('trynow')
escolhe.click()
DMart
  • 2,401
  • 1
  • 14
  • 19