Bottom Page

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  HOWTO? Login DSL Modem with Python Requests: need Click "Apply" Button Webtest 4 2,679 Aug-20-2019, 04:03 PM
Last Post: johnmina
  Python Selenium .click() Loads Error - Works Manually.- Events not Triggered NSearch 24 956 Aug-14-2019, 02:23 PM
Last Post: NSearch
  Selenium click on popup button??? GuJu 7 831 Jul-20-2019, 09:21 AM
Last Post: Nizam
  Ajax and Selenium ankitjindalbti 3 468 May-31-2019, 03:40 PM
Last Post: ankitjindalbti
  Selenium Python migs2173 1 392 May-12-2019, 04:33 AM
Last Post: metulburr
  Error when trying to use Selenium ejected 1 573 Mar-26-2019, 04:53 AM
Last Post: ejected
  wait.until(EC.element_to_be_clickable) failed to click gahhon 4 500 Feb-23-2019, 04:58 AM
Last Post: gahhon
  How to trigger click event on Button without ID/Name gahhon 7 713 Feb-10-2019, 03:06 PM
Last Post: gahhon
  Web scraping (selenium (i think)) Larz60+ 10 1,066 Jan-27-2019, 02:57 AM
Last Post: Larz60+
  selenium timeout metulburr 8 1,005 Jan-14-2019, 03:21 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)