Python Code Snippets
Example code snippets for use in the WayScript Python module.

Using Selenium

The Python module includes the Chrome headless browser, which you can interact with using Selenium. Here is some sample code to do this:
1
from selenium import webdriver
2
3
options = webdriver.ChromeOptions()
4
options.add_argument('--headless')
5
options.add_argument('--no-sandbox')
6
options.add_argument('--disable-dev-shm-usage')
7
options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0')
8
9
browser = webdriver.Chrome(options = options)
10
browser.set_page_load_timeout(30)
11
12
browser.get('https://wayscript.com/')
13
14
ps = browser.page_source
15
print(ps)
16
17
browser.close()
Copied!
Be sure to add the following options:
option.add_argument('--headless') option.add_argument('--no-sandbox') option.add_argument('--disable-dev-shm-usage')

Connecting to a Microsoft SQL Server Database

Use the following code to connect to a publicly-accessible SQL Server database using Python.
1
import pyodbc
2
3
server = 'sqlservertest.us-east-1.rds.amazonaws.com'
4
port = '1430'
5
database = 'WayScriptTest'
6
username = 'Admin'
7
password = 'Pa$word'
8
conx = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};SERVER='+server+','+port+';DATABASE='+database+';Uid='+username+';Pwd='+password)
Copied!
Last modified 1yr ago