Clicking a specific item within a drop down list

DL Neil PythonList at DancesWithMice.info
Tue Apr 2 22:16:03 EDT 2019


Richard,
(this is a Python list, and whilst the question is quite proper - your 
are using Python after all, the answer delves into Selenium and then 
dives into HTML and finally disappears into 'the dark side' of JavaScript!)


On 3/04/19 2:02 PM, rlew2008 at gmail.com wrote:
> Hi dn,
> Thank you kindly for your warm greeting.  Please forgive my lack of forum decorum, I just started Python 2 days ago out of necessity, so I'm still quite unknowledgable about a lot of things.
> I'm working on a person project.  So far, my only resources to date have been forums based off of Google searches, and a YouTube video: https://www.youtube.com/watch?v=rfscVS0vtbw
> This is the code that I have so far:
> from selenium import webdriver
> driver = webdriver.Chrome()
> driver.get("http://financials.morningstar.com/balance-sheet/bs.html?t=XNAS:AAPL&region=usa&culture=en-US")
> import time
> time.sleep(2)
> driver.find_element_by_id("menu_A")
> driver.("Quarterly")
> When I run it, I get this error code:
> selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Quarterly"}
> I've exhausted all of the forum posts I can find on this topic, but I've still been unable to solve it.  Any suggestions on how to solve it are eagerly welcomed.


Python is being helpful. Stop and read the error message: "Unable to 
locate element". What can you learn from the components of that msg?

The error was found by Selenium.
Did you look within their documentation?
Sadly, a quick web-search link to their web site basically repeats the 
err.msg. Big deal!

The subject Selenium is examining is a web page.
In this case the word "element" is not part of Python, nor is it part of 
Selenium (per-se), but is an HTML term.
Have you opened that page in your web-browser? (bring it up again, if 
necessary)

Usually I would suggest using the web-browser to look at HTML elements, 
indeed Firefox's (right-mouse-click) Context Menu offers an "Inspect 
Element" function, or use of the "Developer Tools" (F12). NB 
Chromium/Chrome and presumably the Apple and MSFT products offer similarly.

In this case, may I recommend (first) using the web-browser's "View 
Source" facility (Context or Tool menus) to inspect the HTML code of 
this web page.
What happens when you try to find "Quarterly" on the page?

Yes, it is found, but that's not in the place you expect ("menu_A")!

Return to the web page and now try the "Inspect Element" function.
Ahah, the "quarterly" link content *is* there.
What is going on?

Return to the page source, scroll through to (manually) find the Balance 
Sheet. It's not there either!
Yet, on the web page you can see it with your own eyes!!!???

What is there, around line 259 of the HTML, is a call to a JavaScript 
script.
What did I say about "going over to the dark side"?

You ask Selenium to load a web page. In turn, the browser/driver 
discovers that the web-page/HTML loads other items/files, eg graphics, 
CSS files, and JavaScript code files.

That's where the problem (seems to) lies - and it also explains the 
difference between Firefox's "View Source" facility and the "Developer 
Tools". Selenium works fast. Accordingly, as soon as the HTML page is 
available, it finds "menu_A" and thence attempts to find the "Quarterly" 
link. Unfortunately, the JavaScript needs to be loaded (from a separate 
file on the web server) and executed. You have a "race condition". The 
solution (hopefully - I have not tested it) is to add a delay between 
the page-load and the search-analysis (and possibly again, to allow the 
quarterly analysis to load before commencing the next step...

WebRefs:
https://stackoverflow.com/questions/6936149/how-to-use-find-element-by-link-text-properly-to-not-raise-nosuchelementexcept
https://en.wikipedia.org/wiki/Race_condition


Let us know how you get on...
-- 
Regards =dn



More information about the Python-list mailing list