Search code examples
pythonpython-3.xbrowsersplinter

Python 3.x Editing text areas and copying from fields and not open browser


Had a look Here

No specific answers to my question

I am using splinter and here's what I put into the shell and what returned.

>>> from splinter.browser import Browser
>>> browser = Browser()
>>> browser.visit('http://www.timestampgenerator.com/tools/md5-generator/')
>>> browser.fill('source','hello')
>>> browser.find_by_id('submit').click()
>>> ans = browser.find_by_id('result')
>>> print(ans)
[<splinter.driver.webdriver.WebDriverElement object at 0x00000010DECEE3C8>]

The copying didn't work very well. You can visit This page To get an idea of what I'm in against. What i want to ASK How can I delete text from a certain field, so if I put hello after doing

browser.fill('source','hello')

I might be wanting to do it again without refreshing the page so I need to delete text. How to do that? Also you can see that my attempt to copy text with

>>> ans = browser.find_by_id('result')
>>> print(ans)
[<splinter.driver.webdriver.WebDriverElement object at 0x00000010DECEE3C8>]

failed, would like to know how to do that. Also finally how can I do this operation without opening firefox windowed, perhaps simply as a background process.


Solution

  • To get the text from the 'result' element, you need to print the value of the selected element.

    print(ans.value)
    

    You can make a list with entries you want to fill the 'source' element with, and use a for loop to go through the list. You don't need to delete the previous entry, fill will overwrite the contents it seems.

    Here is an example, had to put in a 1 second wait_time in my case for it to not grab the wrong 'result'.

    from splinter import Browser
    
    with Browser() as browser:
        browser.visit('http://www.timestampgenerator.com/tools/md5-generator/')
        entries = ['hello', 'goodbye']
        for item in entries:
            browser.fill('source', item)
            browser.find_by_id('submit').click()
            browser.is_text_present('result', wait_time=1)
            ans = browser.find_by_id('result')
            print(ans.value)
    

    For a headless webbrowser (not opening a window), check out the splinter documentation. You'll need to get a webdriver of your choice.