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?

Chen W
  • 119
  • 2
  • 12
  • 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. – Eb946207 Dec 05 '18 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? – Eb946207 Dec 05 '18 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? – Chen W Dec 05 '18 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. – Eb946207 Dec 05 '18 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 "} – Chen W Dec 05 '18 at 23:46
  • So whatever `type(driver.find_element_by_link_text("MyFilters "))` is will be different then the `type()` of whatever `.click()` worked for. But for all objects like this one, `.click()` will never work. – Eb946207 Dec 05 '18 at 23:48
  • @EthanK Do you by any chance know why it's not working for that one? Or how else can I click on the button? – Chen W Dec 05 '18 at 23:49
  • Not really, I don't know anything about [tag:selenium], only how python works with functions like `.click()`. I do know that the type of object in this case **is different** then the objects that this *did* work for. – Eb946207 Dec 06 '18 at 00:16

1 Answers1

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.

PixelEinstein
  • 1,713
  • 1
  • 8
  • 17
  • 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? – Chen W Dec 06 '18 at 01:42
  • It probably is, I would use things like `WebDriverWait` conditions, to make sure your element is visible first. [THIS](https://stackoverflow.com/questions/26566799/wait-until-page-is-loaded-with-selenium-webdriver-for-python) post would be helpful with that issue, but it sounds like the click is fixed! So that's good. – PixelEinstein Dec 06 '18 at 01:52