本文整理汇总了Python中selenium.webdriver.PhantomJS.find_element_by_xpath方法的典型用法代码示例。如果您正苦于以下问题:Python PhantomJS.find_element_by_xpath方法的具体用法?Python PhantomJS.find_element_by_xpath怎么用?Python PhantomJS.find_element_by_xpath使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selenium.webdriver.PhantomJS
的用法示例。
在下文中一共展示了PhantomJS.find_element_by_xpath方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AdvertisementAdvancedViewTests
# 需要导入模块: from selenium.webdriver import PhantomJS [as 别名]
# 或者: from selenium.webdriver.PhantomJS import find_element_by_xpath [as 别名]
class AdvertisementAdvancedViewTests(LiveServerTestCase):
def setUp(self):
self.driver = PhantomJS()
self.user = User.objects.create_user('admin', '[email protected]', 'pass')
self.user.save()
self.provider = Provider(
name='provider',
user=self.user,
)
self.provider.save()
self.provider_adverts = mommy.make(Advertisement, _quantity=20, provider=self.provider)
def tearDown(self):
self.driver.quit()
def open(self, url):
self.driver.get("%s%s" % (self.live_server_url, url))
def test_side_ad_display(self):
"""
Test that the side ads display properly
"""
self.open(reverse('advertisements.views.side_ads'))
self.assertEqual(len(self.driver.find_elements_by_xpath("//a")), 4)
self.driver.find_element_by_xpath("//a[1]/img")
self.driver.find_element_by_xpath("//a[2]/img")
self.driver.find_element_by_xpath("//a[3]/img")
self.driver.find_element_by_xpath("//a[4]/img")
self.assertNotEqual(self.driver.find_element_by_xpath("//a[1]").get_attribute("href"), '')
self.assertNotEqual(self.driver.find_element_by_xpath("//a[2]").get_attribute("href"), '')
self.assertNotEqual(self.driver.find_element_by_xpath("//a[3]").get_attribute("href"), '')
self.assertNotEqual(self.driver.find_element_by_xpath("//a[4]").get_attribute("href"), '')
def test_top_ad_display(self):
"""
Test that the top ad displays properly
"""
self.open(reverse('advertisements.views.top_ad'))
self.assertEqual(len(self.driver.find_elements_by_xpath("//a")), 1)
self.driver.find_element_by_xpath("//a/img")
self.assertNotEqual(self.driver.find_element_by_xpath("//a").get_attribute("href"), '')
示例2: Premiumgeneratorlink
# 需要导入模块: from selenium.webdriver import PhantomJS [as 别名]
# 或者: from selenium.webdriver.PhantomJS import find_element_by_xpath [as 别名]
class Premiumgeneratorlink(object):
def __init__(self, url):
self.url = url
self.browser = PhantomJS()
def get_link(self):
try:
self.browser.get('http://premiumgeneratorlink.com/')
self.browser.find_element_by_name('link').send_keys(self.url)
self.browser.find_element_by_xpath('//a[@class="input"]').click()
wdw = WebDriverWait(self.browser, 10)
wdw.until(EC.element_to_be_clickable((By.ID, 'check'))).click()
wdw.until(EC.element_to_be_clickable((By.ID, 'generate'))).click()
link = wdw.until(EC.visibility_of_element_located((By.XPATH, '//form[@class="center"]'))).get_attribute('action')
except (WebDriverException, NoSuchElementException, TimeoutException):
return False
finally:
self.browser.quit()
return link
示例3: render
# 需要导入模块: from selenium.webdriver import PhantomJS [as 别名]
# 或者: from selenium.webdriver.PhantomJS import find_element_by_xpath [as 别名]
def render(gist_id, commit):
block_url = 'http://bl.ocks.org/' + gist_id
d3_block_rec = {'gist_id': gist_id}
try:
driver = PhantomJS()
driver.get(block_url)
time.sleep(RENDER_DELAY) # let it render
fullpage_im = Image.open(BytesIO(driver.get_screenshot_as_png()))
fimb = BytesIO()
fullpage_im.save(fimb, 'png')
d3_block_rec['fullpage_base64'] = base64.b64encode(fimb.getvalue())
d3_block_rec['block_url'] = driver.current_url
except Exception as e:
# we got nothing
with LittlePGer('dbname=' + DB_NAME, commit=commit) as pg:
d3_block_rec['error'] = str(e)
pg.insert('d3_block', values=d3_block_rec)
exit(10)
try:
f = driver.find_element_by_xpath('//iframe')
x, y = int(f.location['x']), int(f.location['y'])
w, h = x + int(f.size['width']), y + int(f.size['height'])
block_im = fullpage_im.crop((x, y, w, h))
bimb = BytesIO()
block_im.save(bimb, 'png')
d3_block_rec['block_base64'] = base64.b64encode(bimb.getvalue())
d3_block_rec['block_size'] = list(block_im.size)
except Exception as e:
# at least we got the fullpage im, save it
with LittlePGer('dbname=' + DB_NAME, commit=commit) as pg:
d3_block_rec['error'] = str(e)
pg.insert('d3_block', values=d3_block_rec)
exit(11)
# all good, save everything
with LittlePGer('dbname=' + DB_NAME, commit=commit) as pg:
pg.insert('d3_block', values=d3_block_rec)
示例4: get_applications_in_page
# 需要导入模块: from selenium.webdriver import PhantomJS [as 别名]
# 或者: from selenium.webdriver.PhantomJS import find_element_by_xpath [as 别名]
def get_applications_in_page(self, scroll_script):
applications = []
driver = None
try:
desired_capabilities = dict(DesiredCapabilities.PHANTOMJS)
desired_capabilities["phantomjs.page.settings.userAgent"] = useragent.get_random_agent(google_prop.user_agent_list_url)
service_args = ['--load-images=no', '--proxy=%s' % (proxy.get_random_proxy(google_prop.proxy_list_url))]
driver = PhantomJS(desired_capabilities=desired_capabilities, service_args=service_args)
# driver = Firefox(firefox_profile=self.fp, proxy=self.proxy)
if self.proxy_test:
driver.get('http://curlmyip.com/')
ip = driver.find_element_by_xpath('//body//pre').text
print('ip : [ ' + ip + ' ]')
pass
else:
driver.get(self.url)
driver.execute_script(scroll_script)
acknowledge = 0
done = False
while not done:
scroll_finished = driver.execute_script("return scraperLoadCompleted")
if scroll_finished:
if acknowledge == self.acknowledgements:
done = driver.execute_script("return scraperLoadCompleted")
pass
else:
acknowledge += 1
pass
pass
else:
acknowledge = 0
pass
time.sleep(5) # Wait before retry
pass
product_matrix = driver.find_elements_by_class_name("card")
for application in product_matrix:
extracted_application = self.extract_application_data(application)
# if extracted_application['app_price'] != -1:
applications.append(extracted_application)
#pass
pass
pass
driver.quit()
pass
except Exception as e:
if driver is not None:
driver.quit()
pass
if self.attempt < self.retries:
self.attempt += 1
time.sleep(10)
print 'retry : url [ ' + self.url + ' ] + | attempt [ ' + str(self.attempt) + ' ] | error [ ' + str(e) + ' ]'
applications = self.get_applications_in_page(scroll_script)
pass
else:
print('fail : url [ ' + self.url + ' ] | error [ ' + str(e) + ' ]')
pass
pass
return applications
pass
示例5: ProviderAdvancedViewTests
# 需要导入模块: from selenium.webdriver import PhantomJS [as 别名]
# 或者: from selenium.webdriver.PhantomJS import find_element_by_xpath [as 别名]
class ProviderAdvancedViewTests(LiveServerTestCase):
def setUp(self):
self.driver = PhantomJS()
self.user = User.objects.create_user('admin', '[email protected]', 'password')
self.user.save()
self.provider = Provider(
name='provider',
user=self.user,
)
self.provider.save()
self.provider_adverts = mommy.make(Advertisement, _quantity=20, provider=self.provider)
self.login()
def tearDown(self):
self.driver.quit()
def open(self, url):
self.driver.get("%s%s" % (self.live_server_url, url))
def login(self):
self.open(settings.LOGIN_URL)
self.driver.find_element_by_id("id_username").send_keys("admin")
self.driver.find_element_by_id("id_password").send_keys("password")
self.driver.find_element_by_css_selector("button.btn.btn-default").click()
self.assertEqual(
self.driver.current_url,
self.live_server_url + reverse('advertisements.views.view_provider_statistics', args=[self.provider.pk]),
)
def test_can_login(self):
"""
Test that the user can login
"""
pass
def test_provider_page_has_all_data(self):
"""
Test that the provider statistics page has all the correct data
"""
self.open(reverse('advertisements.views.view_provider_statistics', args=[self.provider.pk]))
self.assertEqual("Open Ads", self.driver.title)
self.assertIn(
"{0} advertisements".format(self.provider.name),
self.driver.find_element_by_css_selector("h1.page-header").text
)
self.assertIn(
"{0} advertisements in rotation".format(20),
self.driver.find_element_by_css_selector("h1.page-header").text
)
def test_advertisement_page_has_all_data(self):
"""
Test that the advertisement page has all the correct data
"""
for advert in self.provider_adverts:
self.open(reverse('advertisements.views.view_advert_statistics', args=[advert.pk]))
self.assertIn(
"ID number: {0}".format(advert.pk),
self.driver.find_element_by_css_selector("h1.page-header").text,
)
self.driver.find_element_by_css_selector("img")
self.assertEqual("Active", self.driver.find_element_by_xpath("//td[2]/span").text)
self.assertEqual(advert.url, self.driver.find_element_by_link_text(advert.url).text)
self.driver.find_element_by_link_text("Edit URL").click()
self.assertEqual(advert.url, self.driver.find_element_by_id("id_url").get_attribute("value"))
示例6: get_url_files
# 需要导入模块: from selenium.webdriver import PhantomJS [as 别名]
# 或者: from selenium.webdriver.PhantomJS import find_element_by_xpath [as 别名]
def get_url_files(retail, invoice_doc_type, invoice_id, invoice_date, invoice_amount):
retail_invoice_url = RETAIL_INVOICE_URL[retail]
driver = PhantomJS()
driver.get(retail_invoice_url)
# 1 Set doc_type 'select'
try:
select_doc_type = Select(driver.find_element_by_name('txtTipoDte'))
value = RETAIL_INVOICE_DOC_TYPES[retail][invoice_doc_type]['value']
select_doc_type.select_by_value(value)
# name = RETAIL_INVOICE_DOC_TYPES[retail][invoice_doc_type]['name']
# select_doc_type.select_by_visible_text(name)
except Exception:
print 'ERROR: set doc_type select as Boleta'
driver.save_screenshot('screen.png')
return '', ''
time.sleep(5)
# 2 Get recaptcha img url
try:
recaptcha_img = driver.find_element_by_id('recaptcha_challenge_image')
recaptcha_img_url = recaptcha_img.get_attribute('src')
except Exception:
print 'ERROR: get recaptcha image url'
driver.save_screenshot('screen.png')
return '', ''
# 3 Solve recaptcha
v = VisionApi()
recaptcha_value = v.detect_text_from_url(recaptcha_img_url)
if recaptcha_value is None:
print 'ERROR: solving recaptcha image'
driver.save_screenshot('screen.png')
return '', ''
# 4 Fill form
script = u"""
document.getElementsByName('txtFolio')[0].value = '{invoice_id}';
document.getElementsByName('txtFechaEmision')[0].value = '{invoice_date}';
document.getElementsByName('txtMontoTotal')[0].value = '{invoice_amount}';
document.getElementsByName('recaptcha_response_field')[0].value = '{recaptcha_value}';
""".format(
invoice_id=invoice_id,
invoice_date=invoice_date,
invoice_amount=invoice_amount,
recaptcha_value=recaptcha_value,
)
driver.execute_script(script)
# 5 Submit form
try:
driver.find_element_by_name('frmDatos').submit()
except Exception:
print 'ERROR: submitting form'
driver.save_screenshot('screen.png')
return '', ''
# 6 Get url files
try:
xml_a_tag = driver.find_element_by_xpath('//*[@id="Tabla_01"]/tbody/tr[1]/td[2]/p/a[2]')
pdf_a_tag = driver.find_element_by_xpath('//*[@id="Tabla_01"]/tbody/tr[1]/td[2]/p/a[1]')
xml_url = xml_a_tag.get_attribute('href')
pdf_url = pdf_a_tag.get_attribute('href')
except Exception:
print 'ERROR: getting url files'
driver.save_screenshot('screen.png')
return '', ''
# 8 Delete driver session
driver.close()
driver.quit()
return xml_url, pdf_url