I’m trying to write my first script for Selenium/Python, so no doubt this is a very silly question. I’m trying to find the element that says “Club Pilot Airmanship” in the following HTML:
It gives the error: File “C:\Users\RowanB\AppData\Roaming\Python\Python38\site-packages\selenium\w ebdriver\remote\errorhandler.py”, line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:“css selector”,“selector”:".css-api-card-header __title"} (Session info: chrome=84.0.4147.125)
Why is it not finding the element? What do I have to do to fix this?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
See this post to find the backtick on your keyboard. Note: Backticks (`) are not single quotes (’).
As for your question, I cannot see any element within the HTML you have pasted with a class of css-api-card-header__title. Can you point this element out?
With Selenium, I remember having best success with using the XPath. If you are using Chrome, the element XPath is as easy to find as right-clicking an HTML element in the element tree -> Copy -> By XPath.
This exception is thrown by various accessor methods to indicate that the element being requested does not exist . The next() method in Java returns the next element in the iteration or NoSuchElementException if the iteration has no more elements. The solution to this exception is to check whether the next position of an iterable is filled or empty.
As with most programming languages, the Iterator class includes a hasNext() method that returns a boolean indicating if the iteration has anymore elements. If hasNext() returns true, then the next() method will return the next element in the iteration otherwise raise exceptions if the iteration has no more elements.
if(input.hasNextInt() )
number1 = input.nextInt(); // if there is another number
else
number1 = 0; // nothing added in the input