Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
selenium click a span tag
#1
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]

Attached Files

Thumbnail(s)
   
Recommended Tutorials:
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Click on a button on web page using Selenium Pavel_47 7 4,564 Jan-05-2023, 04:20 AM
Last Post: ellapurnellrt
  trying to scrape a span inside a div using beautifulsoup CompleteNewb 2 8,795 Jan-28-2021, 01:20 PM
Last Post: snippsat
  select all the span text with same attribute JennyYang 2 2,096 Jul-28-2020, 02:56 PM
Last Post: snippsat
  Log In Button Won't Click - Python Selenium Webdriver samlee916 2 3,770 Jun-07-2020, 04:42 PM
Last Post: samlee916
  Hyperlink Click is not working in Selenium webdriver rajeev1729 0 1,994 May-02-2020, 11:21 AM
Last Post: rajeev1729
  selenium click in iframe fails 3Pinter 6 5,022 Apr-29-2020, 12:59 PM
Last Post: Larz60+
  Scrap a dynamic span hefaz 0 2,659 Mar-07-2020, 02:56 PM
Last Post: hefaz
  Cannot get contents from ul.li.span.string LLLLLL 8 3,949 Nov-29-2019, 10:30 AM
Last Post: LLLLLL
  Python Selenium .click() Loads Error - Works Manually.- Events not Triggered NSearch 24 11,518 Aug-14-2019, 02:23 PM
Last Post: NSearch
  Selenium click on popup button??? GuJu 7 7,721 Jul-20-2019, 09:21 AM
Last Post: Nizam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020