Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 selenium click a span tag
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
but i keep getting errors such as
Traceback (most recent call last):   File "", line 73, in <module>     clip_all_btns(browser)   File "", 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[""] = chromedriver
    browser = webdriver.Chrome(chromedriver)
    return browser

def login(browser):
    login to account
    username = browser.find_element_by_id("email")
    password = browser.find_element_by_id("password")
    login_attempt = browser.find_element_by_xpath("//*[@type='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);")
        #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):
            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")

browser = setup()



with open('output.txt','w') as f:

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 class="hover">
           <div class="sprite-pod circle">
            <p class="click-text">
             Add to card
            <p class="click-text-sec">
          <div class="media">
           <div class="media-object pull-left">
            <img alt="" src="//"/>
            <div class="show-detail">
             Show Details
             <span class="caret">
           <div class="media-body">
            <p class="pod_summary">
             <span class="eboxtop-img">
             Save $5.00
            <p class="pod_brand">
             Dollar General
            <p class="pod_description">
             when you make a purchase of $30 or more (pre-tax) at a Dollar General Location 12/3/16
            <p aria-label="expiration on 12/03/16" class="pod_expiry">
             Exp: 12/03/16
            <div class="badge">
             <span aria-label="activated coupon" class="activate-text">
             <span aria-label="click to add coupon" class="add-text">
          <div class="sprite-pod clipped-container">
           <div class="clipped-view">
            <span class="box clip-box clip-action">
            <span class="box info pull-right">
          <div class="detail" style="display:none">
            <strong class="sub">
            $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.
each button itself is:
            <span aria-label="click to add coupon" class="add-text">
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


Attached Files Thumbnail(s)
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).
Can return a list so test with:
Try with a more excat mact(CSS selector/xPath) to "span" tag you want to test click on.
And lower case in .Click(),so .click()

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Log In Button Won't Click - Python Selenium Webdriver samlee916 1 204 May-31-2020, 07:23 PM
Last Post: samlee916
  Hyperlink Click is not working in Selenium webdriver rajeev1729 0 123 May-02-2020, 11:21 AM
Last Post: rajeev1729
  selenium click in iframe fails 3Pinter 6 262 Apr-29-2020, 12:59 PM
Last Post: Larz60+
  Scrap a dynamic span hefaz 0 583 Mar-07-2020, 02:56 PM
Last Post: hefaz
  Cannot get contents from LLLLLL 8 512 Nov-29-2019, 10:30 AM
Last Post: LLLLLL
  Python Selenium .click() Loads Error - Works Manually.- Events not Triggered NSearch 24 2,397 Aug-14-2019, 02:23 PM
Last Post: NSearch
  Selenium click on popup button??? GuJu 7 2,031 Jul-20-2019, 09:21 AM
Last Post: Nizam
  Click Element if displayed using Selenium and Python giaco__mar 1 1,348 Dec-27-2018, 06:19 PM
Last Post: metulburr
  Selenium xpath finding gif to click fyec 1 1,264 Jul-27-2018, 05:20 PM
Last Post: snippsat
  Selenium chromedriver and click action Gilles95 4 8,179 Feb-07-2018, 07:28 PM
Last Post: Gilles95

Forum Jump:

Users browsing this thread: 1 Guest(s)