0

This is my first time with both Python and Selenium, and I'm trying to select and click on an element of the navigation bar of this website - stockfetcher.com

I would like to click on the MyFilters tab on the navigation bar. After inspecting the element, I see that the HTML code for that element is

<a href="/myfilters"> "MyFilters " <span class="tab-pill"></span> </a>

And here's what I trying to do

driver.find_element_by_link_text("MyFilters ").click()

And I get this error

AttributeError: 'NoneType' object has no attribute 'click'

Any idea what's wrong? Most examples I watched online did not have "" around whatever is inside the tag. Is that what's messing me up?

8
  • That error means that you can't call .click(), you would get the same error if you replaced .click() with .fsghudghur(). Whatever find_element_by_link_text returns had no click() function. Commented Dec 5, 2018 at 23:13
  • Try running type(driver.find_element_by_link_text("MyFilters ")). See if what is returned is what you want. Also try printing it with print(driver.find_element_by_link_text("MyFilters ")). Is that good as well? Commented Dec 5, 2018 at 23:15
  • @EthanK I added those in my code, but I didn't see anything on the console. So the method of link_text won't work here? Commented Dec 5, 2018 at 23:29
  • Well, if you didn't see anything then it didn't run or you are not looking at the console. And if you got that error, .click() won't just not work here, but .click() won't work anywhere. Check the documentation to make sure .click() exists, if not then make sure you are using the right version. Commented Dec 5, 2018 at 23:32
  • @EthanK .click() is definitely working. Because preceding this I had to click buttons to log in. Actually I see this in the console=>> selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"MyFilters "} Commented Dec 5, 2018 at 23:46

1 Answer 1

2

Link text is sometimes very picky about what you send to it, and I have found that you have to match what the actual link is showing, and not the value following href. Since the site shows MyFilters, try doing this instead:

driver.find_element_by_link_text('MyFilters').click()

Without the space after Filters. I tested on my side and it seems to work.

Also, the error does not match what you have given as an example, it would throw NoSuchElementException, before it would throw that NoneType has no click() event. Either way the above should work for you.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, so this works! But it will not work if I place it right after my 'sign in' click. However, if I sandwich another find_element command between them then the second one will work. Could it be that it is trying to click while the page is loading?
It probably is, I would use things like WebDriverWait conditions, to make sure your element is visible first. THIS post would be helpful with that issue, but it sounds like the click is fixed! So that's good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.