Python: Selenium

I had some issues getting selenium installed with Python 3.6 which I use on my main machine so I decided to install it on my VM that I use with DNS VR for work.  This has the benefit of having a “disposable” system in case I screw it up.  I also have the goal to introduce NatLink into the environment to extend DNS capabilities and enhance reporting/formatting.

 

Steps to get selenium up:

  1. Install python 2.7 from here.
  2. Install selenium with “pip install selenium”
  3. Install the Geckodriver for firefox from here.

Thats it! You can now start playing with selenium.  A good first start is:

from selenium import webdriver

browser = webdriver.Firefox()

browser.get(‘https://triradiate.wordpress.com’)

 

 

Coding Hard Line Breaks into Reports

Problem:  The RIS does not induce hard line breaks or enforce any character limit so text scrolls lengthwise across the screen when viewed.  This makes it very difficult to read a report even then a structured format if there is any prose text whatsoever. The text should be broken into columns if necessary using hardcoded carriage returns.

 

Solution: Python text processing.

 

 

This is my first pass at chopping prose text to the 70 character default used in the textwrap python module.

Python 3.6

from tkinter import Tk
import textwrap

report = Tk()
#Prevent the window from showing
report.withdraw()
uncutText=report.clipboard_get()
#Adds text to clipboard
#report.clipboard_append('Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.')
#Gets clipboard text
uncutText=report.clipboard_get()
#print(uncutText)
cutText = textwrap.wrap(uncutText)
report.clipboard_clear()
#print("\n")
for line in cutText:
    # debug self check
    #print(line)
    #Add carriage return to clipboardfor each line pasted
    report.clipboard_append("\n")
    report.clipboard_append(line)
report.destroy()

This is my first pass at chopping prose text to the 70 character default used in the textwrap python module.

Text Report:

The lateral meniscus shows fraying of the free edge of the body without a tear. The medial meniscus has an oblique undersurface tear of the body extending to the posterior horn near the root. There is subchondral marrow edema associated with the posterior horn route extension of the tear. A developing intraosseous ganglion is present near the torn root.

That is maddening to scroll all the way across the screen just to read some important info.

Processed Text Report:

The lateral meniscus shows fraying of the free edge of the body
without a tear. The medial meniscus has an oblique undersurface tear
of the body extending to the posterior horn near the root. There is
subchondral marrow edema associated with the posterior horn route
extension of the tear. A developing intraosseous ganglion is present
near the torn root.

 

Much improved!

UPDATE: 20170214

The clipboard was not being maintained even though the text was being processed.  On stackexchange, I found two methods to get around this:

http://stackoverflow.com/questions/9322070/unable-to-append-to-clipboard

adding: report.mainloop()  and that put me in an infinite loop because I don’t know what I’m doing and I put it inside my for loop.

or

adding: report.update()  which worked perfectly.

from tkinter import Tk
import textwrap

report = Tk()
#Prevent the window from showing
report.withdraw()
try:
    uncutText=report.clipboard_get()
except:
    uncutText="The Clipboard Was empty"
#Gets clipboard text
uncutText=report.clipboard_get()
print(uncutText)
cutText = textwrap.wrap(uncutText)
report.clipboard_clear()
#print("\n")
for line in cutText:
    #debug self check
    #print(line)
    #Add carriage return to clipboard for each line pasted
    report.clipboard_append("\n")
    report.clipboard_append(line)
    report.update()

report.destroy()

 

 

Next step is to trigger the script from Dragon!