本文整理汇总了Python中splinter.Browser.find_by_tag方法的典型用法代码示例。如果您正苦于以下问题:Python Browser.find_by_tag方法的具体用法?Python Browser.find_by_tag怎么用?Python Browser.find_by_tag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类splinter.Browser
的用法示例。
在下文中一共展示了Browser.find_by_tag方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LoginTestCase
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
class LoginTestCase(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
self.browser = Browser('chrome')
def tearDown(self):
self.testbed.deactivate()
def test_login(self):
self.browser.visit("http://127.0.0.1:8080/")
self.assertEqual(self.browser.find_by_tag("h3").first.text, "Not logged in")
self.browser.find_by_id("submit-login").first.click()
self.assertEqual(self.browser.find_link_by_text("Insurance").first.text, "Insurance")
def test_logout(self):
self.browser.visit("http://127.0.0.1:8080/")
self.assertEqual(self.browser.find_by_tag("h3").first.text, "Not logged in")
self.browser.find_by_id("submit-login").first.click()
self.assertEqual(self.browser.find_link_by_text("Insurance").first.text, "Insurance")
self.browser.find_link_by_text("Log out").first.click()
self.assertEqual(self.browser.find_by_tag("h3").first.text, "Not logged in")
示例2: run
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
def run(self):
"""Run the b0t"""
browser = Browser()
browser.visit(self.url)
try:
while browser.find_by_tag('button').first:
self.process_elements(browser)
browser.find_by_tag('button').first.click()
except ElementDoesNotExist:
pass
示例3: PlayPagesWebTests
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
class PlayPagesWebTests(StaticLiveServerTestCase):
def setUp(self):
self.user1 = UserFactory.build()
self.user1.set_password('abc')
self.user1.save()
self.user2 = UserFactory.build()
self.user2.set_password('123')
self.user2.save()
self.browser1 = Browser()
def tearDown(self):
self.browser1.quit()
def login_helper(self, browser, username, password):
browser.visit(
'%s%s' % (self.live_server_url, '/accounts/login/')
)
browser.fill('username', username)
browser.fill('password', password)
browser.find_by_value('Log in').first.click()
# Test 4
# Check playing single player game
def test_single_player(self):
self.login_helper(self.browser1, self.user1.username, 'abc')
self.browser1.visit(
'%s%s' % (self.live_server_url, '/play/')
)
time.sleep(2)
snippet = self.browser1.find_by_id('type').value
try:
snippet = snippet[:-(
len(snippet.split()[-1]) + 1
)]
for c in snippet[:100]:
self.browser1.type('typed', c)
time.sleep(0.001)
self.browser1.find_by_tag('input')[3].click()
self.browser1.find_by_tag('input').last.click()
self.assertEqual(
self.browser1.url,
'%s%s' % (self.live_server_url, '/scores/')
)
except ElementNotVisibleException:
self.assertTrue(True)
示例4: UserUtils
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
class UserUtils(object):
def __init__(self):
self.config = config.read_config()
self.account = self.config['account']
self.idp_server = self.config['nodes']['idp_node']
# Abort test if esgf-web-fe is not reachable
r = requests.get("https://{0}/esgf-web-fe".format(self.idp_server), verify=False, timeout=1)
assert r.status_code == 200
self.browser = Browser('firefox')
# Mapping user data to fit to web-fe user creation form
self.elements = {'firstName' : self.account['firstname'],
'lastName' : self.account['lastname'],
'email' : self.account['email'],
'userName' : self.account['username'],
'password1' : self.account['password'],
'password2' : self.account['password']}
def check_user_exists(self):
URL = "https://{0}/esgf-web-fe/login".format(self.idp_server)
OpenID = "https://{0}/esgf-idp/openid/{1}".format(self.idp_server, self.account['username'])
# Try to log in
self.browser.visit(URL)
self.browser.find_by_id('openid_identifier').fill(OpenID)
self.browser.find_by_value('Login').click()
# User does not exist if unable to resolve OpenID
if(self.browser.is_text_present("Error: unable to resolve OpenID identifier")):
self.user_exists = False
else:
self.user_exists = True
def create_user(self):
URL = "https://{0}/esgf-web-fe/createAccount".format(self.idp_server)
self.browser.visit(URL)
# Filling the form
for element_name in self.elements:
self.browser.find_by_name(element_name).fill(self.elements[element_name])
self.browser.find_by_value('Submit').click()
# Parsing response
self.response = []
if (self.browser.is_text_present("SUCCESS") == True):
self.response.append("SUCCESS")
else:
self.response.append("FAILURE")
selection = self.browser.find_by_tag('span')
for sel in selection:
if sel.has_class('myerror'):
self.response.append(sel.value)
def exit_browser(self):
self.browser.quit()
示例5: splinter
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
def splinter():
browser = Browser()
url = "http://ehire.51job.com/MainLogin.aspx"
browser.visit(url)
time.sleep(1)
browser.find_by_id('txtMemberNameCN').fill(u'安能聚业')
browser.find_by_id('txtUserNameCN').fill(u'上海安能聚创供应链')
browser.find_by_id('txtPasswordCN').fill('aneqc888')
browser.find_by_id('Login_btnLoginCN').click()
time.sleep(1)
browser.find_by_tag('a').click()
browser.find_by_id('hlResumeSearch').click()
# id 85798642 未公开
# 309554553 未下载
#
browser.find_by_id('txtUserID').fill('6098724')
time.sleep(1)
browser.find_by_id('btnSearchID_leftbtnSearchID').click()
cvTarget = browser.find_by_xpath('//tr/td/p/span/a[@target="_blank"]')
if len(cvTarget) == 0:
print "can not find the cv from this id."
return
cvTarget.click()
allwindows = browser.windows
driver = browser.driver
driver.switch_to_window(allwindows[-1].name)
UndownloadLink = browser.find_by_id('UndownloadLink')
if len(UndownloadLink) == 0:
print "can not find the cv from this id."
else:
UndownloadLink.click()
time.sleep(1)
browser.find_by_id('btnCommonOK').click()
selector = etree.HTML(browser.html)
lines = selector.xpath('//title')
if len(lines) != 0:
print "name:", strip(lines[0].text)
contents = browser.html.encode("utf-8")
print re.findall(re.compile('''<td height="20">电 话:</td><td height="20" colspan="3">(.*?)<span'''), contents)[0]
printre.findall(re.compile('''E-mail:</td><td height="20" colspan="3"><a href="mailto:(.*?)" class="blue">'''),
contents)[0]
winNum = len(allwindows)
for i in range(winNum):
allwindows[winNum - 1 - i].close()
示例6: splinter
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
def splinter(url):
#"""""""""""""""""""""""""MySQL DEF**********************************************
conn = MySQLdb.connect(host='192.168.1.8',user='root',passwd='123123',db='gwycf')
cursor = conn.cursor()#create cursor operate db
#"""""""""""""""""""""""""MySQL DEF**********************************************
data = xlrd.open_workbook('./chafen.xlsx')
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
print nrows
browser = Browser('firefox')
# browser = Browser('chrome')
dir(browser)
browser.visit(url)
time.sleep(5)
count = 0
#<================================================>
for i in range(nrows):
#HaoMa = str(table.row_values(i)[1]).split(".")[0]
name = table.row_values(i)[0]
HaoMa = table.row_values(i)[1]
# epost = table.row_values(i)[2]
browser.find_by_name('TxtName').fill(name)
browser.find_by_name('TxtHaoMa').fill(HaoMa)
browser.find_by_id('btnSubmit').click()
#=================获取页面数据=====================
epost = browser.find_by_tag('td')[10].value
ecode = browser.find_by_tag('td')[14].value
xingce = browser.find_by_tag('td')[16].value
shenlun = browser.find_by_tag('td')[18].value
jiafen = browser.find_by_tag('td')[20].value
zongfen = browser.find_by_tag('td')[22].value
#=================获取页面数据======================
query = u"insert into info values('%s','%s','%s','%s','%s','%s','%s','%s',0)" % (name,HaoMa,epost,ecode,xingce,shenlun,jiafen,zongfen)
print count,query
cursor.execute(query.encode('utf-8')) #原始数据可以根据gbk运行无错,现在改成utf8
conn.commit()
browser.back()
count = count +1
cursor.close()
conn.commit()
conn.close()
示例7: TestViews
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
class TestViews(unittest.TestCase):
def setUp(self):
""" Test setup """
self.browser = Browser("phantomjs")
# Set up the tables in the database
Base.metadata.create_all(engine)
# Create an example user
self.user = models.User(name="Alice", email="[email protected]",
password=generate_password_hash("test"))
session.add(self.user)
session.commit()
self.process = multiprocessing.Process(target=app.run)
self.process.start()
time.sleep(1)
def test_add_post(self):
log= logging.getLogger("unittest.TestCase")
################################## Login as Alice
#self.browser.visit("http://0.0.0.0:8080/login") # original line
self.browser.visit("http://127.0.0.1:5000/login")
self.browser.fill("email", "[email protected]")
self.browser.fill("password", "test")
button = self.browser.find_by_css("button[type=submit]")
button.click()
#self.assertEqual(self.browser.url, "http://0.0.0.0:8080/") # original line
# self.assertEqual(self.browser.url, "http://127.0.0.1:5000/") # ask sam about this line
############################################ add a test post #####################
self.browser.visit("http://127.0.0.1:5000")
self.browser.click_link_by_partial_href('add')
self.browser.fill("title", "post test1 title")
self.browser.fill("content", "post test1 content")
button = self.browser.find_by_css("button[type=submit]")
button.click()
post_found = self.browser.find_by_tag('h1').value #cheated here - made template title h2. how do we access? index?
#post_found = self.browser.find_by_text('post test1 title').value - didnt work
log.debug( "FIRSTH1= %r", post_found )
self.assertEqual(post_found, "post test1 title")
def tearDown(self):
""" Test teardown """
# Remove the tables and their data from the database
self.process.terminate()
session.close()
engine.dispose()
Base.metadata.drop_all(engine)
self.browser.quit()
示例8: Retreiver
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
class Retreiver():
def __init__(self, folder):
self.aux = Auxiliary()
self.folder = folder
self.tickers = None
def click(self, destination):
try:
self.browser.find_by_text(destination).first.click()
except splinter.exceptions.ElementDoesNotExist:
self.browser.click_link_by_text(destination)
def retreive(self):
print ('Please enter the period for retrieval.')
raw_dates = input ('Dates in European format: dd/mm/yyyy\n>')
eurodates = self.aux.date_parse(raw_dates)[0]
dates = self.aux.european_dates_to_american(eurodates)
raw_tickers = input ('Tickers:\n>')
self.tickers = self.aux.parse_tickers(raw_tickers)
self.browser = Browser('chrome')
for ticker in self.tickers:
self.browser.visit('https://beta.finance.yahoo.com/quote/%s/history' % ticker)
time.sleep(5)
input_boxes = self.browser.find_by_tag('input')
for i in range(0,6):
input_boxes[i+2].fill(dates[i]) #we need 3-8 inputs
self.click('Apply')
download_link = self.browser.find_link_by_text('Download data').first
response = requests.get(download_link['href'])
with open('%s//%s.csv' % (self.folder, ticker), 'wb') as f:
f.write(response.content)
self.browser.quit()
def put_together(self):
if not self.tickers:
self.tickers = []
for f in os.listdir(self.folder):
self.tickers.append(f[:-4])
target = openpyxl.Workbook()
sheet = target.active
sheet.append(self.tickers)
for filename in os.listdir(self.folder):
source = open('%s//%s' %(self.folder, filename), 'r', encoding='utf-8')
sheet = target.create_sheet()
sheet.title = filename[:-4] #strip out the extension
for line in source:
sheet.append(self.aux.parse_comma_separated_line(line))
source.close()
target.save('Historical_data_together.xlsx')
示例9: __authorize
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
def __authorize(self):
b = Browser('chrome')
b.visit("http://box-token-generator.herokuapp.com/")
if b.find_link_by_href('set_client_credentials'):
b.visit('http://box-token-generator.herokuapp.com/set_client_credentials')
time.sleep(2)
b.find_by_id('login').first.fill('[email protected]')
b.find_by_id('password').first.fill('dharit1250')
b.find_by_name('login_submit').first.click()
b.find_by_id('consent_accept_button').first.click()
code = b.find_by_tag('h4')[1].text
self.client = box.BoxClient(code)
b.quit()
示例10: ScoresWebTests
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
class ScoresWebTests(StaticLiveServerTestCase):
def setUp(self):
self.user1 = UserFactory.build()
self.user1.set_password('abc')
self.user1.save()
self.user2 = UserFactory.build()
self.user2.set_password('123')
self.user2.save()
self.userscore1 = UserScores(
user=self.user1,
wpm_gross=110,
wpm_net=100,
mistakes=8
)
self.userscore1.save()
self.userscore2 = UserScores(
user=self.user2,
wpm_gross=100,
wpm_net=90,
mistakes=10
)
self.userscore2.save()
self.match = Matches(winner=self.userscore1, loser=self.userscore2)
self.match.save()
self.browser = Browser()
def tearDown(self):
self.browser.quit()
def login_helper(self, username, password):
self.browser.visit(
'%s%s' % (self.live_server_url, '/accounts/login/')
)
self.browser.fill('username', username)
self.browser.fill('password', password)
self.browser.find_by_value('Log in').first.click()
# Test 11
# Check anon get of /scores/
def test_anon_get_scores(self):
self.browser.visit('%s%s' % (self.live_server_url, '/scores/'))
self.assertEqual(
self.browser.url,
'%s%s' % (self.live_server_url, '/accounts/login/?next=/scores/')
)
# Test 12
# Check anon get of /scores/match_score
def test_anon_get_match_score(self):
self.browser.visit('%s%s' % (
self.live_server_url,
'/scores/match_score')
)
self.assertEqual(
self.browser.url,
'%s%s' % (
self.live_server_url,
'/accounts/login/?next=/scores/match_score'
)
)
# Test 13
# Check scores for user
def test_user_for_scores(self):
self.login_helper(self.user1.username, 'abc')
self.browser.visit('%s%s' % (self.live_server_url, '/scores/'))
self.assertEqual(
self.browser.find_by_tag('strong')[2].text, self.user1.username
)
self.assertEqual(
self.browser.find_by_tag('strong')[3].text, str(
self.userscore1.wpm_net
)
)
self.assertEqual(
self.browser.find_by_tag('strong')[4].text, self.user2.username
)
self.assertEqual(
self.browser.find_by_tag('strong')[5].text, str(
self.userscore2.wpm_net
)
)
示例11: Institution
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
browser.find_by_name('addScenario').first.click()
browser.fill('scName', countryTypeList[conIndex]+typeaName+igType)
browser.type('scEffDate', '\b\b\b\b\b\b\b\b\b\b')
browser.type('scEffDate', '2015-10-31')
browser.find_by_name('update').first.click()
browser.find_link_by_text('Obligor').first.click()
# choose the companyType type
element = browser.find_by_name('companyType').first
element.select(str(cType))
browser.fill('obligorName', companyName)
browser.find_by_name('ObligorSearch').first.click()
browser.find_by_tag('tbody').first.find_by_tag('form').first.find_by_tag('table')[2].find_by_tag('a')[0].click()
# select "B-III counterpaty type" to be "corporate"
element = browser.find_by_name('counterPartyType').first
element.select('1')
# select "Classification re Asset Value Correlation" to be "Non-Financial Institution (N)"
element = browser.find_by_name('avc').first
element.select('4')
# select proper IG according to the IG type
element = browser.find_by_name('obligorIgCode').first
if igType == 'cap':
element.select('99')
browser.find_by_name('UpdateButton').first.click()
elif igType == 'floor':
element.select('30')
示例12: getCVContacts
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
def getCVContacts(self, cvID):
cvID = "85798642" # 不公开
cvID = "307661274"
# cvID = "6098724"
# cvID = "318657201"
from splinter import Browser
browser = Browser()
url = "http://ehire.51job.com/MainLogin.aspx"
browser.visit(url)
time.sleep(1)
browser.find_by_id('txtMemberNameCN').fill(u'安能聚业')
browser.find_by_id('txtUserNameCN').fill(u'上海安能聚创供应链')
browser.find_by_id('txtPasswordCN').fill('aneqc888')
browser.find_by_id('Login_btnLoginCN').click()
time.sleep(1)
browser.find_by_tag('a').click()
selector = etree.HTML(browser.html)
cvDownableNum = selector.xpath('//span[@id ="Navigate_AvalidResumes"]/a/b')[0].text
if cvDownableNum == "0":
self.log.fatal("id:%s can not be down, as to cvDownableNum == 0." % (cvID))
browser.quit()
return "0\n\ncvDownableNum is 0."
browser.find_by_id('hlResumeSearch').click()
browser.find_by_id('txtUserID').fill(cvID)
time.sleep(1)
browser.find_by_id('btnSearchID_leftbtnSearchID').click()
cvTarget = browser.find_by_xpath('//tr/td/p/span/a[@target="_blank"]')
if len(cvTarget) == 0:
self.log.fatal("can not find the cv from this id:%s." % (cvID))
browser.quit()
return "0\n\ncan not find the cv from this id."
cvTarget.click()
allwindows = browser.windows
browser.driver.switch_to_window(allwindows[-1].name)
UndownloadLink = browser.find_by_id('UndownloadLink')
if len(UndownloadLink) != 0:
UndownloadLink.click()
time.sleep(1)
browser.find_by_id('btnCommonOK').click()
selector = etree.HTML(browser.html)
contents = browser.html.encode("utf-8")
winNum = len(allwindows)
for i in range(winNum):
allwindows[winNum - 1 - i].close()
browser.quit()
lines = selector.xpath('//title')
name = ""
if len(lines) != 0:
name = strip(lines[0].text)
try:
phone = \
re.findall(re.compile('''<td height="20">电 话:</td><td height="20" colspan="3">(.*?)<span'''), contents)[0]
except:
phone = "not supplied"
try:
eMail = \
re.findall(re.compile('''E-mail:</td><td height="20" colspan="3"><a href="mailto:(.*?)" class="blue">'''),
contents)[0]
except:
eMail = "not supplied"
if not isinstance(name, unicode):
name = name.decode("utf-8")
if not isinstance(phone, unicode):
phone = phone.decode("utf-8")
result = "1\n\nname:%s\tphone:%s\teMail:%s" % (name, phone, eMail)
self.log.fatal(result)
return result
示例13: EnvironmentError
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
if browser.is_text_present('Congratulations. This browser is configured to use Tor.'):
print "Yes, using TOR :)"
else:
print "No, not using TOR :("
raise EnvironmentError('Not using TOR')
browser.visit('https://tasonline.gotilc.com/GTPublicWeb/MainWeb/GageSelect.aspx')
# assert_that(browser.is_text_present('Select Car Mark', wait_time=5))
car_mark_select_list = browser.find_by_id('TRNContentPlaceHolder_ddCarMark_FilterSec')
assert_that(len(car_mark_select_list), equal_to(1))
car_mark_select = car_mark_select_list[0]
print car_mark_select
assert_that(car_mark_select.tag_name, equal_to(u'select'))
options = browser.find_by_tag('option')
for option in options:
car_mark_select.select(option.text)
car_mark_filter = browser.find_by_id('TRNContentPlaceHolder_ddCarMark_FilterSec')
print option.text
car_mark_select.select('ACTX')
# assert browser.is_text_present('Loading...'), u'Text not found'
assert_that(browser.is_text_not_present('Loading...', wait_time=6))
car_mark_select_list = browser.find_by_id('TRNContentPlaceHolder_ddCarMark_FilterSec')
assert_that(len(car_mark_select_list), equal_to(1))
car_mark_select = car_mark_select_list[0]
assert_that(car_mark_select.tag_name, equal_to(u'select'))
示例14: TestViews
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
class TestViews(unittest.TestCase):
def setUp(self):
""" Test setup """
self.browser = Browser("phantomjs")
# Set up the tables in the database
Base.metadata.create_all(engine)
# Create an example user
self.user = models.User(name="Alice", email="[email protected]",
password=generate_password_hash("test"))
session.add(self.user)
session.commit()
self.process = multiprocessing.Process(target=app.run)
self.process.start()
time.sleep(1)
def testLoginCorrect(self):
self.browser.visit("http://127.0.0.1:5000/login")
self.browser.fill("email", "[email protected]")
self.browser.fill("password", "test")
button = self.browser.find_by_css("button[type=submit]")
button.click()
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/")
def testLoginIncorrect(self):
self.browser.visit("http://127.0.0.1:5000/login")
self.browser.fill("email", "[email protected]")
self.browser.fill("password", "test")
button = self.browser.find_by_css("button[type=submit]")
button.click()
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/login")
def testAddEditPost(self):
self.browser.visit("http://127.0.0.1:5000/login")
self.browser.fill("email", "[email protected]")
self.browser.fill("password", "test")
button = self.browser.find_by_css("button[type=submit]")
button.click()
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/")
self.browser.visit('http://127.0.0.1:5000/post/add')
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/post/add")
self.browser.fill("title", "First Post")
self.browser.fill("content", "Hello World!")
button = self.browser.find_by_css("button[type=submit]")
button.click()
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/")
self.browser.click_link_by_text('Edit Post')
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/post/1/edit")
self.browser.fill("title", "Edited First Post")
self.browser.fill("content", "Hello Universe!")
button = self.browser.find_by_css("button[type=submit]")
button.click()
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/")
self.assertEqual(self.browser.find_by_tag('h1').first.value, "Edited First Post")
#divs = self.browser.find_by_tag("div")
#myList = []
#if "Hello Universe!" in divs:
#myList.append("Hello Universe!")
#self.assertEqual(myList[0], "Hello Universe!")
def testAddDeletePost(self):
self.browser.visit("http://127.0.0.1:5000/login")
self.browser.fill("email", "[email protected]")
self.browser.fill("password", "test")
button = self.browser.find_by_css("button[type=submit]")
button.click()
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/")
self.browser.visit('http://127.0.0.1:5000/post/add')
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/post/add")
self.browser.fill("title", "First Post")
self.browser.fill("content", "Hello World!")
button = self.browser.find_by_css("button[type=submit]")
button.click()
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/")
self.browser.click_link_by_text('Delete Post')
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/post/1/delete")
button = self.browser.find_by_css("button[type=submit]")
button.click()
self.assertEqual(self.browser.url, "http://127.0.0.1:5000/")
self.assertEqual(len(self.browser.find_by_tag('h1')),0)
#divs = self.browser.find_by_tag("div")
#myList = []
#if "Hello Universe!" in divs:
#myList.append("Hello Universe!")
#self.assertEqual(myList[0], "Hello Universe!")
def tearDown(self):
""" Test teardown """
# Remove the tables and their data from the database
self.process.terminate()
session.close()
engine.dispose()
Base.metadata.drop_all(engine)
self.browser.quit()
示例15: scrape
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_tag [as 别名]
def scrape():
# Create dictionary to return
return_dict = {}
# Create initial browser object
executable_path = {'executable_path': '/Users/joshchung/Bootcamp/chromedriver'}
browser = Browser('chrome', **executable_path, headless=False)
# Scrape NASA Mars news
url = 'https://mars.nasa.gov/news/'
browser.visit(url)
html = browser.html
soup = bs(html, 'lxml')
results = soup.find('li', class_="slide")
article_date = results.find('div', class_="list_date").text
article_title = results.find('div', class_="content_title").text
article_teaser = results.find('div', class_="article_teaser_body").text
return_dict.update({'article_date':article_date,
'article_title':article_title,
'article_teaser':article_teaser})
# Scrape JPL image
url = 'https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars'
browser.visit(url)
html = browser.html
soup = bs(html, 'lxml')
results = soup.find_all('article', class_="carousel_item")
url_string = results[0].get('style')
url_string = url_string.split("url('")
url_string = url_string[1].split("');")
url_string = url_string[0]
img_url = 'https://www.jpl.nasa.gov' + url_string
return_dict.update({'img_url':img_url})
# Scrape Twitter
url = 'https://twitter.com/marswxreport'
browser.visit(url)
html = browser.html
soup = bs(html, 'lxml')
last_tweet = soup.find('p', class_="tweet-text").text
last_tweet = last_tweet.replace('\n', ' ')
return_dict.update({'last_tweet':last_tweet})
# Scrape Mars facts
url = 'https://space-facts.com/mars/'
tables = pd.read_html(url)
mars_df = tables[0]
mars_df.columns = ['Statistic','Values']
mars_df = mars_df.set_index('Statistic')
mars_table = mars_df.to_html()
mars_table = mars_table.replace('\n', '')
return_dict.update({'mars_table':mars_table})
# Scrape Mars hemisphere images
url = 'https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars'
mars_urls = {}
for x in range(0,4):
browser.visit(url)
links = browser.find_by_tag('h3')
links[x].click()
html = browser.html
soup = bs(html, 'lxml')
downloads = soup.find('div', class_="downloads")
dl_links = downloads.find_all('a')
img_link = dl_links[0].get('href')
dld_link = dl_links[1].get('href')
title = soup.find('h2', class_="title").text
mars_urls.update({
f"marsimg_{x}" : img_link,
f"marstitle_{x}": title,
f"marsdld_{x}": dld_link
})
browser.back()
return_dict.update(mars_urls)
# Return dictionary when function is run
return return_dict