Python Forum

Full Version: selenium click a span tag
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am having trouble clicking a span tag that is a button, but not using button html tag at all.

Most example i see are doing something like
Quote:find_tag('tag_name').Click()
but i keep getting errors such as
Error:
Traceback (most recent call last):   File "test1.py", line 73, in <module>     clip_all_btns(browser)   File "test1.py", line 63, in clip_all_btns     element.Click() AttributeError: 'WebElement' object has no attribute 'Click'
full code. Specifically from the function on line 51 clip_all_btns. In which it loops all the <span class add-text> code for each button
#!/usr/bin/python3 env

from selenium import webdriver
import time
import os

def setup():
    '''
    setup webdriver and create browser
    '''
    chromedriver = "/home/metulburr/chromedriver"
    os.environ["webdriver.chrome.driver"] = chromedriver
    browser = webdriver.Chrome(chromedriver)
    return browser

def login(browser):
    '''
    login to account
    '''
    browser.get("https://dg.coupons.com/signin/") 
    time.sleep(1) 
    username = browser.find_element_by_id("email")
    password = browser.find_element_by_id("password")
    username.send_keys("MY_EMAIL")
    password.send_keys("MY_PASS")
    login_attempt = browser.find_element_by_xpath("//*[@type='submit']")
    login_attempt.submit()
    
def get_btns(browser):
    '''
    return a list of buttons from span tag class name to add coupon
    '''
    return browser.find_elements_by_class_name('add-text')

def make_all_btns_visable(browser):
    '''
    show all buttons by scrolling down
    '''
    count = 0
    for _ in range(100):
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(1)
        #browser.FindElement(By.XPath("//div[@class='pod ci-grid recommended desktop ']//div[@class='media']//div[@class='media-body']//div[@class='badge']//span[@class='add-text']")).Click()
        clip_btns = get_btns(browser)
        print('Scrolling {} buttons into view'.format(len(clip_btns)))
        if count >= len(clip_btns):
            break
        else:
            count = len(clip_btns)
            
def clip_all_btns(browser):
    '''
    clip all coupons by clicking all add-text span tag
    '''
    clip_btns = get_btns(browser)
    for btn in clip_btns:
        element = browser.find_element_by_tag_name("span")
        print(element)
        print(element.text)
        element.Click()

browser = setup()
login(browser)

time.sleep(3) 
browser.get('https://dg.coupons.com/coupons/')
time.sleep(3)

make_all_btns_visable(browser)
clip_all_btns(browser)
    


with open('output.txt','w') as f:
    f.write(browser.page_source)
#print(browser.page_source)


where each button section consists of:
         <div class="pod ci-grid recommended desktop " data-agent="desktop" data-code="X508365" data-omscid="19635134" data-podid="05b5dd4b-c143-4ec3-8031-4e79ebeecb98" data-source="">
          <div class="pod-icon Dollar General ">
          </div>
          <div class="hover">
           <div class="sprite-pod circle">
            <p class="click-text">
             Add to card
            </p>
            <p class="click-text-sec">
            </p>
           </div>
          </div>
          <div class="media">
           <div class="media-object pull-left">
            <img alt="" src="//cdn.cpnscdn.com/static.coupons.com/ext/bussys/cpa/pod/89/247/389247_9c7d2c59-368a-4203-92cd-0c99622a439e.gif"/>
            <div class="show-detail">
             Show Details
             <span class="caret">
             </span>
            </div>
           </div>
           <div class="media-body">
            <p class="pod_summary">
             <span class="eboxtop-img">
             </span>
             Save $5.00
            </p>
            <p class="pod_brand">
             Dollar General
            </p>
            <p class="pod_description">
             when you make a purchase of $30 or more (pre-tax) at a Dollar General Location 12/3/16
            </p>
            <p aria-label="expiration on 12/03/16" class="pod_expiry">
             Exp: 12/03/16
            </p>
            <div class="badge">
             <span aria-label="activated coupon" class="activate-text">
              ✓
             </span>
             <span aria-label="click to add coupon" class="add-text">
              <span>
               Add
              </span>
              +
             </span>
            </div>
           </div>
          </div>
          <div class="sprite-pod clipped-container">
           <div class="clipped-view">
            <span class="box clip-box clip-action">
             <i>
              ✓
             </i>
             Added
            </span>
            <span class="box info pull-right">
             <i>
              ⇱
             </i>
             Info
            </span>
           </div>
          </div>
          <div class="detail" style="display:none">
           <div>
            <strong class="sub">
             Details:
            </strong>
            $30 or more (pretax) calculated after all other Dollar general discounts. Limit one per customer.  We reserve the right to limit use to normal retail purchases. No cash value. Coupon excludes: gift cards, phone cards, prepaid financial cards, prepaid wireless handsets, Rug Doctor rentals, propane, e-cigarettes, tobacco and alcoholic beverages.
           </div>
          </div>
         </div>
each button itself is:
            <span aria-label="click to add coupon" class="add-text">
             <span>
              Add
             </span>
             +
            </span>
I tried clicking both span tags but getting the same result.

the entire html of one logged in of the page where all the buttons are
http://codepad.org/b0mi7mYG

[anchor=to_attachment][/anchor]
Do not do any loop,before you have tested that it work on first span tag.

Code under will return the first match of span tag,
make sure that this is the right span tag(can be more in source).
browser.find_element_by_tag_name("span")
Can return a list so test with:
browser.find_element_by_tag_name("span")[0].click()
Try with a more excat mact(CSS selector/xPath) to "span" tag you want to test click on.
Edit:
And lower case in .Click(),so .click()