2

how do I click on the first search result on metacritc's search bar?

this is what I have so far:

self.search_element = self.driver.find_element_by_name("search_term")
self.search_element.clear()
self.search_element.send_keys(self.game_line_edit.text())

self.link_to_click = self.driver.find_element_by_name("search_results_item")
self.link_to_click.click()

# self.game.setText(self.driver.find_element("search_results_item"))
self.game_line_edit.setText("")
self.driver.close()

but I'm getting this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"search_results_item"}

I realize selenium can't find the link but I am not sure what element it is

this is the HTML I'm trying to click on:

<a class="search_results_item" href="https://www.metacritic.com/game/pc/into-the-breach">
    <span class="metascore_w score_outstanding">90</span>
    <span class="title" data-mctitle="Into the Breach"><b>Into the</b> Breach</span>
    <span class="type secondary">PC Game</span>
    <span class="separ secondary">,</span>
    <span class="date secondary">2018</span>
</a>

can someone help?

Thanks!

JeffC
  • 22,180
  • 5
  • 32
  • 55
littlejiver
  • 255
  • 2
  • 13
  • When you post HTML and/or code please take a minute to use a beautifier like http://jsbeautifier.org/ or your IDE to properly format everything. If you need help properly formatting it on the site, see the formatting help link in the sidebar of the question editor. It makes it a LOT easier to read which makes your question more likely to get answered. Thanks! – JeffC Oct 08 '18 at 22:11
  • You're using `find_element_by_name` to find an element by class name. Have you tried using `find_element_by_class_name` instead? – Parker Beck Oct 08 '18 at 22:15

2 Answers2

0

You are searching by name when you are referring to a class. Instead use a CSS selector, e.g.

.find_element_by_css_selector(".search_results_item")

.search_results_item indicates a class with the name 'search_results_item'.

If that doesn't work, you probably need a wait. See this answer for more info.

JeffC
  • 22,180
  • 5
  • 32
  • 55
0

If you're fine with using xPath, you can select by index.

(//a[contains(@class,'search_results_item')])[1]

@JeffC is also correct as well. You should not select by name because this element has no name. At the very least, select by class or tag.

Michiel Bugher
  • 1,005
  • 1
  • 7
  • 23