Weird Python Selenium Button Click Behaviour
Question: The part I'm trying to click:

    
    
    <ul class="btns right">
    <li><a href="javascript:void(0)" onclick="hr_expand_event_tab_all(&quot;&quot;)" class="expand-all" id="btn_expand_all_10580503">View All Cards</a></li>
    </ul>
    

Pretty straightforward I thought. But I seem to be missing something.

**Question is now updated a little further down the page. The xpath isn't the
problem I've tried with corrected xpath and it's the same as using the class
name. CSS was hiding several versions of the button but a common.exception is
being thrown on the ones it does actually find with xpath or the class name.**

I've checked the page is loaded properly and the element is there. I have a
check to wait until the full page is loaded and it screenshots to be sure.

    
    
    loadbutton = Driver.find_element_by_xpath("//a[@class='expand-all']")
    

Gives:

    
    
    <class 'selenium.common.exceptions.ElementNotVisibleException'>
    

So I tried to find an onclick with the anchor:

    
    
    loadbutton = Driver.find_element_by_xpath("//li[contains(@onclick, 'View All Cards')]")
    

With the same outcome. I've tried a bit of regex to catch the id variations as
well but I'm not sure where I'm going wrong here. There's an onlick and it is
loaded but I can't see to find it.

I'd appreciate anyone who can show me what I'm doing wrong on this one.

/Update:

Turns out there's multiple versions of the button some are visible and others
are not.

I looped:

    
    
    loadbutton = Driver.find_elements_by_xpath("//a[@class='expand-all']")
    for button in loadbutton:
      print "button found"
    

It turned up multiple results. The earlier ones are hidden but the ones at the
end are certainly showing on my browser and the screenshot. So I expected the
early ones to fail and added a .click() with a try: except: and they all
failed still. Didn't expect that.

Further update:

So I ran this:

    
    
    loadbutton = Driver.find_elements_by_xpath("//a[@class='expand-all']")
    for button in loadbutton:
      print "button found"
      try:
        button.click()
      except:
        e = sys.exc_info()[0]
        print e
    

The first couple gave me this:

    
    
    <class 'selenium.common.exceptions.ElementNotVisibleException'>
    

OK expected the CSS is hiding it. The last two which are displaying gave this:

    
    
    <class 'selenium.common.exceptions.WebDriverException'>
    

So it can see them. It won't click them. "Common exception" doesn't seem
overly helpful.


Answer: Try this xpath (updated with code block SO site removed my *)

`//*[contains(concat(' ', @class, ' '), ' btns right ')]//*[contains(concat('
', @class, ' '), ' expand-all ') and contains(text(), 'View All Cards')]`

Provide some wait for the element to be clickable(implicit is recommended).

I have Used Only for java , But I refered here for python
[here](http://selenium-python.readthedocs.io/waits.html) it may help!!

    
    
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    button = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[contains(concat(' ', @class, ' '), ' btns right ')]//*[contains(concat(' ', @class, ' '), ' expand-all ') and contains(text(), 'View All Cards')]')))
    button.click()
    
    
    Even if the above thing fails, try this
    

form these links [link1](http://stackoverflow.com/questions/7794087/running-
javascript-in-selenium-using-python) and
[link2](http://stackoverflow.com/questions/27927964/selenium-element-not-
visible-exception)

    
    
    driver.execute_script("document.getElementsByClassName('expand-all')[0].click();")
    

inject an artificial CLICK on the desired element, remove(comment) all other
codes

May be ur app falls under link2 OP :)

