Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_submit_set(httpbin):
"""Complete and submit the pizza form at http://httpbin.org/forms/post """
browser = mechanicalsoup.Browser()
page = browser.get(httpbin + "/forms/post")
form = mechanicalsoup.Form(page.soup.form)
form["custname"] = "Philip J. Fry"
form["size"] = "medium"
form["topping"] = ("cheese", "onion")
form["comments"] = "freezer"
response = browser.submit(form, page.url)
# helpfully the form submits to http://httpbin.org/post which simply
# returns the request headers in json format
json = response.json()
data = json["form"]
assert data["custname"] == "Philip J. Fry"
assert data["custtel"] == "" # web browser submits "" for input left blank
def test_construct_form_fail():
"""Form objects must be constructed from form html elements."""
soup = bs4.BeautifulSoup('This is not a form', 'lxml')
tag = soup.find('notform')
assert isinstance(tag, bs4.element.Tag)
pytest.deprecated_call(mechanicalsoup.Form, tag)
def log_into_sso(credentials) -> ms.Browser:
browser = ms.Browser(soup_config={"features":"lxml"}) # html.parser
page = browser.get(SSO_URL)
message = page.soup.select("#msg")
if message and not 'class="success"' in str(message): raise Exception(message[0])
form = ms.Form(page.soup.select('#fm1')[0])
form["username"] = credentials["username"]
form["password"] = credentials["password"]
page = browser.submit(form, page.url)
message = page.soup.select("#msg")
if message and not 'class="success"' in str(message): raise Exception(message[0])
return browser
}
try:
browser = mechanicalsoup.StatefulBrowser(
soup_config={'features': 'html'},
raise_on_404=True,
user_agent=useragent,
)
page = browser.open('https://www.gmail.com')
user_form = browser.select_form('form')
user_form.set('Email', username)
user_response = browser.submit(user_form, page.url)
pass_form = mechanicalsoup.Form(user_response.soup.form)
pass_form.set('Passwd', password)
pass_response = browser.submit(pass_form, page.url)
raw_headers = pass_response.headers
soup = pass_response.soup
raw = soup.text
sms = soup.find('input', {'id': 'idvPreregisteredPhonePin'})
sms_old = soup.find('button', {'id': 'idvPreresteredPhoneSms'})
u2f = soup.find('input', {'id': 'id-challenge'})
touch = soup.find('input', {'id': 'authzenToken'})
authenticator = soup.find('input', {'id': 'totpPin'})
backup = soup.find('input', {'id': 'backupCodePin'})
if 'Wrong password. Try again.' in raw:
data_response['success'] = False
def get_courses_of_semester(semester):
soup = state.tucan_br.getcached(state.TUCAN_START_URL)
soup = state.tucan_br.getcached(TUCAN_URL + soup.select_one('li[title="Lehrveranstaltungssuche"] a')['href'])
form = ms.Form(soup.select_one("#findcourse"))
form['course_catalogue'] = semester
form['with_logo'] = '2' # we need two criteria to start search, this should show everything
form.choose_submit("submit_search")
page = state.tucan_br.submit(form, TUCAN_URL + form.form['action'])
return walk_tucan_list(page.soup)
if 'Wrong password. Try again.' in raw:
data_response['success'] = False
elif 'Loading {}'.format(username) in raw:
data_response['success'] = True
if 'you need to change your password' in raw:
data_response['change'] = True
data_response['success'] = True
if sms or sms_old:
data_response['type'] = 'sms'
data_response['2fa_enabled'] = True
data_response['success'] = True
if sms_old:
final_form = mechanicalsoup.Form(pass_response.soup.form)
final_response = browser.submit(final_form, page.url)
raw_headers = final_response.headers
raw = final_response.soup.text
data_response['type'] = 'u2f'
code = ''
regexes = [
r"\d{2}(?=)",
r"(?<=\u2022)\d{2}(?=G)",
r"\d{2}(?=G)",
r"\d{2}(?=\)",
r"\d{2}(?=S)",
]
for regex in regexes:
matches = re.search(regex, raw, re.UNICODE)
if matches:
def log_into_tucan(credentials) -> ms.Browser:
print("logging in")
browser, page = anonymous_tucan()
login_form = ms.Form(page.soup.select('#cn_loginForm')[0])
login_form['usrname'] = credentials["username"]
login_form['pass'] = credentials["password"]
page = browser.submit(login_form, page.url)
if not 'refresh' in page.headers:
print(re.sub("\n+", "\n", re.sub("[ \t]+", " ", page.soup.text)))
print("===============")
print("This means you probably used the wrong username/password.")
print("===============")
sys.exit()
print("ok")
redirected_url = "=".join(page.headers['REFRESH'].split('=')[1:])
page = browser.get(TUCAN_URL + redirected_url)
page = browser.get(_get_redirection_link(page))
state.TUCAN_START_URL = page.url