0

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.

PoweredByCoffee
  • 1,153
  • 3
  • 14
  • 25
  • Seems like the button is hidden using css. can you see the button when your getting this page in your browser ? – Tony May 09 '16 at 11:42
  • **//*/a[contains(text(), 'View All Cards')]** try this xpath, your xpath construction seems wrong – theRoot May 09 '16 at 11:47
  • Why all this bother with xpath when the button has an id? – e4c5 May 09 '16 at 11:49
  • @e4c5 Considering the number in the id I'm guessing it might be an auto-generated id. – RemcoW May 09 '16 at 11:51
  • ID seems to be dynamic..framework generated..... – theRoot May 09 '16 at 11:51
  • I used the id as well with a bit of regex because it varies with the same issue I think you're on to something Tony. I looped with find elements and it triggered a few times so there's multiple buttons some show and some don't I guess it's trying to click the ones which don't. Doesn't matter which it actually clicks though so I'm thinking loop through with an exception handler and keep clicking until it doesn't break. – PoweredByCoffee May 09 '16 at 11:52
  • BTW **//li[contains(@onclick, 'View All Cards')]** is wrong...the text value is View All Cards not 'onclick' value.. – theRoot May 09 '16 at 11:55
  • Oh that makes sense... cheers guys my xpath sucks. I don't think that's specifically what is going wrong here though. Updated the question there's multiple buttons but they all fail on .click() – PoweredByCoffee May 09 '16 at 11:58
  • you should wait until your page fully rendered – Hassan Mehmood May 09 '16 at 12:01
  • Page is fully rendered. It displays exactly as my browser does. – PoweredByCoffee May 09 '16 at 12:01
  • Not fully rendered, try make some function to check whether its completely rendered or not. This is not something you can confirm with naked eye. – Hassan Mehmood May 09 '16 at 12:03
  • I have two functions making sure the page has rendered and it's triggering on the buttons just won't click them. Rendering isn't the issue here unfortunately. – PoweredByCoffee May 09 '16 at 12:06
  • And FYI during runtime some JS functions may add classes on elements ... at that time **//a[@class='expand-all']** fails – theRoot May 09 '16 at 12:10
  • Updated the question again. I don't know if this is an xpath issue looping through with the class names gives the number of buttons I'd expect. It fails to click the ones hidden by css but the ones that are there just throw a "common exception" when I ask it to click them. – PoweredByCoffee May 09 '16 at 12:16
  • try XPATH "//a[contains(@class, 'expand-all')]" – Hassan Mehmood May 09 '16 at 12:27
  • I've tried the working xpath from below it gets the same result as using the class name. Issue doesn't seem to be finding the buttons it can't find the ones hidden by css but throws an exception on the ones which are visible. – PoweredByCoffee May 09 '16 at 12:28

1 Answers1

2

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 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 and link2

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 :)

Community
  • 1
  • 1
theRoot
  • 571
  • 10
  • 33
  • Thanks for the suggestion. It's throwing InvalidSelectorException and that xpath is way beyond me so I've no chance of debugging. That said just updated the question again I don't think the problem is finding them looping through the ones hidden by css fail because they're not found when it comes to clicking but the later ones just throw a common exception. That says to me it's finding them using class just isn't able to click them for some "common" reason. – PoweredByCoffee May 09 '16 at 12:15
  • Works and impressive xpath but gets the exact same results as using the class name unfortunately. – PoweredByCoffee May 09 '16 at 12:26
  • Tried it. Literally left it on a 10 minute wait at one point. It's rendered on the page it's finding all the buttons, exception handling the ones css hides and just throwing a common.exception to the ones it can see but for some reason doesn't want to click.() I haven't seen anything like this before. – PoweredByCoffee May 09 '16 at 12:52
  • Cheers for the suggestion though. – PoweredByCoffee May 09 '16 at 12:52
  • You're a wonderful human being. Worked a charm I never knew that was a thing. – PoweredByCoffee May 09 '16 at 13:04
  • Thanks :) if it helped plz accept the answer .. happy to help :) – theRoot May 09 '16 at 13:09