本文整理汇总了Python中splinter.Browser.find_by_xpath方法的典型用法代码示例。如果您正苦于以下问题:Python Browser.find_by_xpath方法的具体用法?Python Browser.find_by_xpath怎么用?Python Browser.find_by_xpath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类splinter.Browser
的用法示例。
在下文中一共展示了Browser.find_by_xpath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: qd
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
def qd():
## browser = Browser('firefox'
## , profile='D:\UsersDocuments\FFmissions')
browser = Browser('chrome')
browser.visit(juchaozhi)
time.sleep(1)
browser.find_by_name('username').fill(username)
browser.find_by_name('password').fill(password)
browser.find_by_name('button').click()
time.sleep(1)
if browser.is_text_present(username, wait_time=1):
print ('login success!')
logging.info('login success!')
browser.visit('http://best.pconline.com.cn/qiandao.html')
time.sleep(1)
## 添加今天是否有签到的判断,主要判断口id="JBar",或者判断CSS中的元素,如.signStatus,.isgnUps
if browser.find_by_css('.signStatus').visible:
print ('you had signed!')
logging.info('you had signed!')
browser.quit()
else:
browser.find_by_xpath('//*[@id="JSignBox"]/a').click()
time.sleep(1)
print ('mission success!')
logging.info('mission success!')
browser.quit()
else:
print ('login fail!!!')
logging.info('login fail!!!')
示例2: main
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
def main(argv):
try:
opts, args = getopt.getopt(sys.argv[1:], "f:")
except getopt.GetoptError as err:
sys.exit(2)
global test_config
config_file_name = None
for o, a in opts:
if o == "-f":
config_file_name = a
else:
assert False, "unhandled option"
if config_file_name is None:
usage(argv)
sys.exit(1)
config_file = open(config_file_name, "r")
test_config = json.load(config_file)
browser = Browser()
browser.visit(test_config['url'])
e = browser.find_by_xpath("//div[@id='treeNode_account']/div/span")
e.click()
e = browser.find_by_xpath("//div[@id='treeNode_account.AdminAccount']/div/span")
e.click()
e = browser.find_by_xpath("//div[@id='treeNode_account.AdminAccount.ChangePass']/div/span[3]/span[2]")
time.sleep(1)
e.click()
browser.find_by_id('id_new_password').fill(test_config['password'])
browser.find_by_id('id_new_password2').fill(test_config['password'])
browser.find_by_id('btn_PasswordChangeForm_Ok_label').click()
示例3: member_new
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
def member_new(emails,link,password):
browser=Browser('chrome')
for n in emails:
email=n.rstrip()+"@sgsaas.net"
browser.visit(link);
browser.fill('age',"33");
browser.fill('firstname',"Sam");
browser.fill('lastname',"wang");
browser.find_by_name('email').last.fill(email);
browser.fill('telephone',3984020);
browser.fill('address_1',"Bukit Batok Avenue")
browser.fill('city',"Singapore");
browser.fill('postcode',658080);
browser.find_by_xpath('//select[@id="input-country"]//option[@value="188"]').click();
browser.find_by_xpath('//select[@id="input-zone"]//option[@value="4231"]').click();
browser.find_by_name('password').last.fill(password);
browser.fill('confirm',password);
browser.find_by_name('agree').click();
browser.find_by_css('.btn-primary').click();
sleep(1)
print email+" has registered!"
browser.quit()
return;
示例4: UserTest
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
class UserTest(StaticLiveServerTestCase):
def setUp(self):
check_permissions()
self.username = "antonio"
create_user(self.username)
self.browser = Browser()
self.browser.visit(self.live_server_url)
def test_signup(self):
signup_url = settings.SIGNUP_URL
self.browser.click_link_by_partial_href(signup_url)
username = "andres"
password = "andres"
email = "[email protected]"
signup(self.browser, username, password, email)
user_exists = exists_user(username)
self.assertTrue(user_exists)
user = get_user(username)
self.assertEquals(user.username, username)
# self.assertEquals(user.password, password)
self.assertEquals(user.email, email)
document_list_url = self.live_server_url + reverse("documents.views.list_documents")
self.assertEquals(self.browser.url, document_list_url)
profile_xpath = "/html/body/div/div[1]/div/ul[2]/li[4]/a"
profile_link = self.browser.find_by_xpath(profile_xpath)
self.assertEquals(profile_link.value, "@{}".format(username))
# import time; time.sleep(3)
self.browser.quit()
def test_signin(self):
login_url = settings.LOGIN_URL
self.browser.click_link_by_partial_href(login_url)
username = self.username
password = self.username
login(self.browser, username, password)
document_list_url = self.live_server_url + reverse("documents.views.list_documents")
self.assertEquals(self.browser.url, document_list_url)
profile_xpath = "/html/body/div/div[1]/div/ul[2]/li[4]/a"
profile_link = self.browser.find_by_xpath(profile_xpath)
self.assertEquals(profile_link.value, "@{}".format(username))
# import time; time.sleep(3)
self.browser.quit()
示例5: get_buyback_price
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
def get_buyback_price(isbn):
browser = Browser('phantomjs')
url = "http://onlinebuyback.mbsbooks.com/index.php?jde=5920"
browser.visit(url)
search = browser.find_by_xpath('//form[@id="frmSearch"]/textarea[@name="FVISBN"]').first.fill(isbn)
submit = browser.find_by_xpath('//form[@id="frmSearch"]/div[@class="btn"]/input').click()
try:
buyback_price = browser.find_by_xpath('//span[@class="priceback"]/b').value
except:
buyback_price = None
return buyback_price
示例6: parser
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
class parser(object):
def __init__(self):
self.browser = Browser('phantomjs')
def get_text_by_xpath(self, xpath):
return self.browser.find_by_xpath(xpath).first.text
def get_html_by_xpath(self, xpath):
return self.browser.find_by_xpath(xpath).first.html
def download_page(self, url):
self.browser.visit(url)
return self.browser.html
def __del__(self):
self.browser.quit
示例7: WebKit
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
class WebKit(object):
'''WebKit引擎'''
def __init__(self):
self.tag_attr_dict={'*':'href',
'embed':'src',
'frame':'src',
'object':'data'}
def extract_links(self,url):
'''提取页面中链接'''
self.browser=Browser("phantomjs")
try:
self.browser.visit(url)
except Exception as e:
return
for tag,attr in self.tag_attr_dict.items():
link_list=self.browser.find_by_xpath('//%s[@%s]' % (tag,attr))
if not link_list:
continue
for link in link_list:
link=link.__getitem__(attr)
if not link:
continue
link=link.strip()
if link=='about:blank' or link.startwith('javascript:'):
continue
if not link.startwith('http'):
link=parse.urljoin(url,link)
yield link
示例8: Webkit
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
class Webkit(object):
""
def __init__(self):
self.tag_attr = {
'*': 'href',
'frame': 'src',
'iframe': 'src',
'object': 'src'
}
def get_links(self, url):
links = []
self.browser = Browser('phantomjs')
self.browser.visit(url)
for tag, attr in self.tag_attr.viewitems():
llinks = self.browser.find_by_xpath('//%s[@%s]'% (tag, attr))
if not llinks:
continue
for link in llinks:
link = link.__getitem__(attr)
if not link:
continue
if link == 'about:blank' or link.startswith('javascript:'):
continue
if not link.startswith('http:'):
link = urlparse.urljoin(url, link)
links.append(link)
return links
def close(self):
self.browser.quit()
示例9: run
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
def run(self):
"""Run the b0t"""
browser = Browser(self.browser)
browser.visit(self.url)
try:
while browser.find_by_xpath(self.next_button_xpath):
self.process_elements(browser)
browser.find_by_xpath(self.next_button_xpath).last.click()
except ElementDoesNotExist:
pass
try:
window = browser.windows[0]
window.close()
except:
pass
示例10: test_add_name
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
def test_add_name(self):
driver =Browser('firefox')
#navigate to webpage
driver.visit('http://www.ranorex.com/web-testing-examples/vip/')
#enter First name
driver.find_by_xpath('//*[@id="'"FirstName"'"]').fill('Prasanth')
#Enter Last name
driver.find_by_xpath('//*[@id="'"LastName"'"]').fill('Patil')
#Selecting the category
driver.select_id('Category','Music')
driver.choose('Gender','male')
driver.find_by_xpath('//*[@id="'"Add"'"]').click()
#def test_load(self):
driver.find_by_xpath('//*[@id="'"Load"'"]').click()
#def test_save(self):
driver.find_by_xpath('//*[@id="'"Save"'"]').click()
示例11: netflix
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
def netflix(sentence, args):
netflix_config = config.load_config("netflix")
if netflix_config:
username = netflix_config['username']
password = keyring.get_password("netflix", username)
profile_name = netflix_config["profile_name"]
if username and password:
#TODO: change this to zope.testbrowser once it's working in the frontend
chrome_path = config.load_config("chrome_path")
executable_path = {'executable_path': chrome_path}
browser = Browser("Chrome")
browser.visit("https:///netflix.com/Login")
email_field = browser.find_by_name("email")
password_field = browser.find_by_name("password")
sign_in_xpath = '//*[@id="appMountPoint"]/div/div[2]/div/form[1]/button'
sign_in_button = browser.find_by_xpath(sign_in_xpath)
email_field.fill(username)
password_field.fill(password)
sign_in_button.click()
if browser.is_text_present(profile_name):
profile_button = browser.find_by_text(profile_name)
profile_button.click()
#Use ent code to find out if there's a named work of art that was detected
#search_tab_xpath = '//*[@id="hdPinTarget"]/div/div[1]/div/button'
#search_tab_xpath = '//*[@id="hdPinTarget"]/div/div[1]/div/button/span[1]'
search_tab_xpath = '//*[@id="hdPinTarget"]/div/div[1]/div/button'
search_tab = browser.find_by_xpath(search_tab_xpath)
search_tab.click()
if "netflix" in sentence:
if "netflix play "in sentence:
show = sentence.split("netflix play ")[1]
else:
show = sentence.split("netflix ")[1]
#search_field = browser.find_by_text("Titles, people, genres")[0]
search_field_xpath = '//*[@id="hdPinTarget"]/div/div[1]/div/div/input'
search_field = browser.find_by_xpath(search_field_xpath)
search_field.fill(show)
show_card_xpath = '//*[@id="title-card-undefined-0"]'
show_card = browser.find_by_xpath(show_card_xpath)
show_card.click()
play_icon_xpath = '//*[@id="title-card-undefined-0"]/div[2]/a/div/div'
play_icon = browser.find_by_xpath(play_icon_xpath)
play_icon.click()
play_button_xpath = '//*[@id="70279852"]/div[2]/a/div/div'
play_button = browser.find_by_xpath(play_button_xpath)
play_button.click()
#chromecast_button_xpath = '//*[@id="netflix-player"]/div[4]/section[2]/div[7]/div[2]/button'
#chromecast_button = browser.find_by_xpath(chromecast_button_xpath)
#chromecast_button.click()
return "Done"
else:
return "Profile {0} could not be found on the netflix page".format(str(profile_name))
else:
return "Netflix username and password could not be retrieved from config and keyring"
else:
return "Netflix config not found"
示例12: splinter
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [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()
示例13: AddTask
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
class AddTask(TestCase):
def setUp(self):
self._browser = Browser('django')
def test_add_task(self):
filename = os.path.join(os.path.dirname(__file__),
'../../docs/features/add_task.feature')
run(filename, self, verbose=True)
def step_user_exists(self, username):
r'user "([^"]+)" exists'
user = UserFactory.build(username=username)
user.is_staff = True
user.set_password(username)
user.save()
def step_I_visit_page_as_logged_user(self, page, username):
r'I visit "([^"]+)" as logged user "([^"]+)"'
self._browser.visit('/admin/')
self._browser.fill('username', username)
self._browser.fill('password', username)
self._browser.find_by_value('Log in').first.click()
self._browser.visit(page)
def step_I_enter_value_in_field(self, value, field):
r'I enter "([^"]+)" in field "([^"]+)"'
self._browser.fill(field, value)
def step_I_press(self, button):
r'I press button "([^"]+)"'
self._browser.find_by_name(button).first.click()
def step_I_see_task_on_tasks_list(self, task):
r'I see task "([^"]+)" on tasks list'
task_on_list = self._browser.find_by_xpath('//ul[@id="todo"]/li[contains(., "%s")]' % task)
self.assertTrue(task_on_list)
示例14: main
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
def main(argv):
try:
opts, args = getopt.getopt(sys.argv[1:], "f:")
except getopt.GetoptError as err:
sys.exit(2)
global test_config
config_file_name = None
for o, a in opts:
if o == "-f":
config_file_name = a
else:
assert False, "unhandled option"
if config_file_name is None:
usage(argv)
sys.exit(1)
config_file = open(config_file_name, "r")
test_config = json.load(config_file)
browser = Browser()
browser.visit(test_config['url'])
# log in
browser.find_by_id('id_username').fill(test_config['username'])
browser.find_by_id('id_password').fill(test_config['password'])
browser.find_by_id('dijit_form_Button_0_label').click()
l = browser.find_by_xpath("//div[@id='treeNode_reboot']/div")
time.sleep(2)
l.first.click()
# The button labelled Cancel in the code is actually the button which does the Reboot!
l = browser.find_by_id("btn_Reboot_Cancel")
time.sleep(2)
l.first.click()
time.sleep(5)
browser.quit()
示例15: __init__
# 需要导入模块: from splinter import Browser [as 别名]
# 或者: from splinter.Browser import find_by_xpath [as 别名]
class Google:
def __init__(self,headless=True):
if headless:
self.browser=Browser('phantomjs',
user_agent='Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
desired_capabilities={'loadImages': False})
else:
self.browser=Browser()
self.browser.driver.set_page_load_timeout(45)
self.url = 'http://www.google.co.in/search?{}'.format
# (urlencode(dict(tbm='isch',q=keyword)))
def __enter__(self):
return self
def search(self,keyword):
try:
self.browser.visit(self.url(urlencode(dict(tbm='isch',q=keyword))))
except TimeoutException:
return None
# self.browser.fill('q', keyword)
# button = self.browser.find_by_name('btnG')
# button.click()
p=self.browser.find_by_xpath('//div[@data-ri="0"]/a')[0]
# p=self.browser.find_by_css('div.rg_el:nth-child(1) > a:nth-child(1)')
# print p
try:
imgurl = parse_qs(urlparse(p['href']).query)['imgurl'][0]
# print imgurl
return imgurl
except Exception as e:
print e,'error here'
return None
def teardown(self):
self.browser.quit()
def __exit__(self, exc_type, exc_value, traceback):
self.teardown()