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
  Python Selenium .click() Loads Error - Works Manually.- Events not Triggered NSearch 24 959 Aug-14-2019, 02:23 PM
Last Post: NSearch
  Selenium click on popup button??? GuJu 7 834 Jul-20-2019, 09:21 AM
Last Post: Nizam
  Click Element if displayed using Selenium and Python giaco__mar 1 607 Dec-27-2018, 06:19 PM
Last Post: metulburr
  Selenium xpath finding gif to click fyec 1 861 Jul-27-2018, 05:20 PM
Last Post: snippsat
  Selenium chromedriver and click action Gilles95 4 6,175 Feb-07-2018, 07:28 PM
Last Post: Gilles95
  Error in Selenium: CRITICAL:root:Selenium module is not installed...Exiting program. AcszE 1 976 Nov-03-2017, 08:41 PM
Last Post: metulburr
  Click dropdown menu option with Selenium PyChrome AcszE 3 2,158 Oct-26-2017, 10:07 PM
Last Post: metulburr
  Click on a Drop-down button with selenium Python AcszE 3 2,922 Oct-25-2017, 03:13 PM
Last Post: vardhhan
  Click in the (…) icon in youtube with selenium PyChrome AcszE 6 4,112 Oct-19-2017, 02:45 AM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)