简体   繁体   中英

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.

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.

I found out that '\\r' can substitute '\\n', because the following code also works:

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

It works with the type function as well:

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

It appears to be implemented by Selenium itself because the following code which directly calls Selenium commands also behaves the same:

b = Browser() 
b.visit('http://reddit.com')
element = b.driver.find_element_by_css_selector('[name={}]'.format('q'))
element.send_keys('intp\r')

This post mentions it as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM