Search code examples
pythonseleniumsplinter

How to click "return" using Splinter


I cannot figure out how to click "return" when I make Splinter library to input text into reddit's main search bar and since it has no button to search, I probably must click "return"

I saw a similar question here but it did not work for my case and also the case the person wrote the answer for.

class NavigationPage(object):

    def __init__(self, br):
        self.br = br
        self.url = "http://reddit.com"

    @property
    def retrieve_reddit_search_bar(self):
        """
        Retrieves search bar by it's name
        """
        return self.br.find_by_name("q")

    def search(self, search_term):
        self.retrieve_reddit_search_bar.first.fill(search_term) # fills search bar
        self.br.execute_script("document.getElementsByName('q')[0].submit()")

It fails with traceback during second statement in search method. If someone knows how to do it, can you also show me how to apply this "return" click business in all websites? I imagine they execute similar javascript to process search request.


Solution

  • Alright, that seemed interesting. The thought popped out of nowhere but here it is:

    "Return" key click is equivalent to '\n' character. Which means that every search term must be ended with a new line character. By doing this, the return key is automatically clicked and I'm taken to search results in reddit!

    So, the command would look like:

    b = Browser()
    b.visit('http://reddit.com')
    b.fill('q', 'intp\n')
    

    And you're taken to search results as selenium/splinter fills the search term.