I'm trying to click on an element on this page:
url = 'https://finance.yahoo.com/quote/GOOG?ltr=1'
driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_link_text('Financials')
At this point I would like to click on 'Cash Flow', 'Balance Sheet', or 'Quarterly'. I know these buttons have been loaded because I can extract them using BeautifulSoup from the page source. But when I try to do it using Selenium:
driver.find_element_by_link_text('Cash Flow')
driver.find_element_by_link_text('Balance Sheet')
driver.find_element_by_link_text('Quarterly')
All return 'Unable to locate element' except for 'Quarterly' which returns an element but its the one sitting above the graph and not the one above the table which is what I'm interested in.
I think this is due to being in the wrong iframe, and I have located all iframes:
driver.find_elements_by_tag_name('iframe')
which returns 9 elements. But I'm having trouble figuring which iframe the elements I want to click on belong to. I also went through the iframes sequentially and still couldn't find the elements I'm interested in.
you need switch to correct iframe - all of them has different IDs (or some other tags) in java it looks like this (for some random iframe id)
I just checked in the website, they (the elements you are looking for) are NOT in any iframe tag.
Following code worked for me (changed to xpath, no need to switch):
Note: It might be the reason that for "Financials", parent tag is
a
which represent a link, but for other elements (Cash Flow, Balance sheet), parent tag isdiv
which is not a link tag. sofind_element_by_link_text
might not have been worked.Switching between iframes:
You have to switch to the frame in which the element is present before we try to identify it.
Lets assume, your element is inside 3 iframes as follows:
Now, if you want to identify CashFlow which is inside the three iFrames:
Note: I used Frame References instead of indexes as you mentioned there are 9 iFrames. so, using indexes would be confusing. If you can't identify frameElement, then only go for indices.
Reference:
Yes this helped resolve the issue. Thank you very much. There were 2 frames on this webpage (0) and (1). I added the line "driver.switch_to.frame(1)". Below is a copy of the code I used and it has fixed the issue I encountered...