2

I am quite new to python selenium and I am trying to click on a button which has the following html structure:

<a href="#" onclick="loadCalendarsForDateFn('jmpto1605')"><span class="jumpToMonthMM">May</span><span class="jumpToMonthYY">2016</span></a><br/>

I would like to be able to click the "May 2016" button above. Can someone guide me how to do that?

Thanks in advance!

damingzi
  • 622
  • 6
  • 25

2 Answers2

2

You can check both month and year with an XPath:

//a[span[@class="jumpToMonthMM"] = "May" and
    span[@class="jumpToMonthYY"] = "2016"]
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
  • Thanks a lot! I found the link button! One followup question is when I issue the click() command, it just didn't jump to the May 2016. `elem = browser.find_element_by_xpath('//a[span[@class="jumpToMonthMM"] = "May" and span[@class="jumpToMonthYY"] = "2016"]')` `print('Found It!')` `elem.click()` – damingzi Mar 25 '16 at 20:33
  • @damingzi great! Do you mean nothing happens when you issue a `click()`? What if you would [move to the element and then click](http://selenium-python.readthedocs.org/api.html#module-selenium.webdriver.common.action_chains): `ActionChains(driver).move_to_element(elem).click().perform()`? Don't forget the import: `from selenium.webdriver.common.action_chains import ActionChains`. – alecxe Mar 25 '16 at 20:43
  • Thanks for your continuous replies. But the ActionChains click didn't work neither. I am thinking if the elem "May 2016" is the correct one to click. Do you have any clue? – damingzi Mar 25 '16 at 20:58
  • @damingzi could you please elaborate this problem into a separate question? This might make more sense since more people may help. Make sure to provide all the necessary details. Thanks! – alecxe Mar 25 '16 at 20:59
  • Thanks! I found a workaround for click(). I substituted .click() with .send_keys("\n"), the latter one does almost the same thing :) – damingzi Mar 25 '16 at 21:23
  • Would you be so kind to have a look at http://stackoverflow.com/questions/43435984/radio-button-does-not-get-clicked-in-selenium-python ? – Jan Apr 17 '17 at 14:44
1

Alexce has a good solution but you could also look for the onclick attribute with loadCalendarsForDateFn('jmpto1605'):

find_element_by_xpath("""//a[@onclick ="loadCalendarsForDateFn('jmpto1605')"]""")

The 05 and 16 obviously correspond to May and 2016 as so I think it is safe to assume it is unique.

Padraic Cunningham
  • 168,988
  • 22
  • 228
  • 312
  • Thank you Padraic for your input :) I tried similar approach below, and it also worked! `find_element_by_xpath(".//a[contains(@onclick, 'jmpto1605')]")` – damingzi Mar 25 '16 at 21:16