Search Through a Dropdown Menu and Select Element Via Text

Viewed 261

I am trying to change the Statement Type with selenium on this website, from Annual to Quarterly:

What I want

I've got the x-path of the "Quarterly" element here:

//*[@id="menu_A"]/ul/li[3]/a

...and this is my code:

quarterly_select = driver.find_element_by_xpath('//*[@id="menu_A"]')
quarterly_select.click()
quarterly_select = driver.find_element_by_xpath('//*[@id="menu_A"]/ul/li[2]/a')
quarterly_select.click()

Now, I know I can just select the third element of the list and be done, but some companies don't report semi-annually, and as such, the "Quarterly" element is the second in the dropdown sometimes, and the third other times.

So, I was thinking that I could just search for the dropdown element that has the text "Quarterly" in it instead, and so this is what I've come up with so far

//*[@id="menu_A"]/ul/li[text()="Quarterly"]/a

But this isn't working and throws the following error:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="menu_A"]/ul/li[text()="Quarterly"]/a"}

I'm not sure what to do. How can I locate the "Quarterly" element of this dropdown?

1 Answers

If you change

//*[@id="menu_A"]/ul/li[text()="Quarterly"]/a
                                           ↑↑

to

//*[@id="menu_A"]/ul/li/a[text()="Quarterly"]
                       ↑↑

...it seems to work just fine. Note that the text "Quarterly" in within an anchor, not a list item:

Structure of the dropdown menu:

<li>
  <a>Annual</a> <-- Selected element; initially "Annual", 
                    but changes to whatever item is selected
  <ul>
    <li>
      <a>Annual</a>
    </li>
    <li>
      <a>Semi-annual</a>
    </li>
    <li>
      <a>Quarterly</a> <-- What you want to click on. 
                           As you can see, "Quarterly" 
                           is contained within an anchor.
    </li>
  </ul>
</li>

That's why you should have .../li/a[text...] instead of .../li/[text...]/a

demo

Full code:

from selenium import webdriver

driver = webdriver.Chrome()

url = "http://financials.morningstar.com/income-statement/is.html?t=ORTX"
driver.get(url)

quarterly_select = driver.find_element_by_xpath('//*[@id="menu_A"]')
quarterly_select.click()
quarterly_select = driver.find_element_by_xpath('//*[@id="menu_A"]/ul/li/a[text()="Quarterly"]')
quarterly_select.click() 
Related