def test_english_to_english_page_source():
"""
service_args: to prevent ssl v3 error
:return: Test in the page_source if the national flag changed from english to english
"""
driver = webdriver.PhantomJS(service_args=["--ignore-ssl-errors=true"])
driver.get(ROOT + PATH + LANGUAGE["ENGLISH"])
driver.get(ROOT)
driver.refresh()
try:
html_content = driver.page_source
assert_in(TEST_ID["ENGLISH"], html_content)
assert_not_in(TEST_ID["GERMAN"], html_content)
finally:
driver.close()
python类PhantomJS()的实例源码
def test_english_to_german_page_source():
"""
service_args: to prevent ssl v3 error
:return: Test in the page_source if the national flag changed from english to german
"""
driver = webdriver.PhantomJS(service_args=["--ignore-ssl-errors=true"])
driver.get(ROOT + PATH + LANGUAGE["GERMAN"])
driver.get(ROOT)
driver.refresh()
try:
html_content = driver.page_source
assert_in(TEST_ID["GERMAN"], html_content)
assert_not_in(TEST_ID["ENGLISH"], html_content)
finally:
driver.close()
def test_german_to_german_page_source():
"""
service_args: to prevent ssl v3 error
:return: Test in the page_source if the national flag changed from german to german
"""
driver = webdriver.PhantomJS(service_args=["--ignore-ssl-errors=true"])
driver.get(ROOT + PATH + LANGUAGE["GERMAN"])
driver.get(ROOT)
driver.refresh()
try:
html_content = driver.page_source
assert_in(TEST_ID["GERMAN"], html_content)
assert_not_in(TEST_ID["ENGLISH"], html_content)
finally:
driver.close()
def test_german_to_english_page_source():
"""
service_args: to prevent ssl v3 error
:return: Test in the page_source if the national flag changed from german to english
"""
driver = webdriver.PhantomJS(service_args=["--ignore-ssl-errors=true"])
driver.get(ROOT + PATH + LANGUAGE["ENGLISH"])
driver.get(ROOT)
driver.refresh()
try:
html_content = driver.page_source
assert_in(TEST_ID["ENGLISH"], html_content)
assert_not_in(TEST_ID["GERMAN"], html_content)
finally:
driver.close()
def test_english_to_german_cookies():
"""
service_args: to prevent ssl v3 error
cookies[len(cookies) - 1].get("value"): because the value of the language is always a dictionary
at the last place of cookies.
:return: Test in the cookies if the language changed from english to german
"""
driver = webdriver.PhantomJS(service_args=["--ignore-ssl-errors=true"])
driver.get(ROOT + PATH + LANGUAGE["GERMAN"])
driver.get(ROOT)
driver.refresh()
try:
cookies = driver.get_cookies()
language_value = cookies[len(cookies) - 1].get("value")
if language_value is not None:
assert_in(LANGUAGE["GERMAN"], language_value)
assert_not_in(LANGUAGE["ENGLISH"], language_value)
else:
raise Exception("Cookie language value is empty")
finally:
driver.close()
def test_german_to_german_cookies():
"""
service_args: to prevent ssl v3 error
cookies[len(cookies) - 1].get("value"): because the value of the language is always a dictionary
at the last place of cookies.
:return: Test in the cookies if the language changed from german to german
"""
driver = webdriver.PhantomJS(service_args=["--ignore-ssl-errors=true"])
driver.get(ROOT + PATH + LANGUAGE["GERMAN"])
driver.get(ROOT)
driver.refresh()
try:
cookies = driver.get_cookies()
language_value = cookies[len(cookies) - 1].get("value")
if language_value is not None:
assert_in(LANGUAGE["GERMAN"], language_value)
assert_not_in(LANGUAGE["ENGLISH"], language_value)
else:
raise Exception("Cookie language value is empty")
finally:
driver.close()
def test_german_to_english_cookies():
"""
service_args: to prevent ssl v3 error
cookies[len(cookies) - 1].get("value"): because the value of the language is always a dictionary
at the last place of cookies.
:return: Test in the cookies if the language changed from german to english
"""
driver = webdriver.PhantomJS(service_args=["--ignore-ssl-errors=true"])
driver.get(ROOT + PATH + LANGUAGE["ENGLISH"])
driver.get(ROOT)
driver.refresh()
try:
cookies = driver.get_cookies()
language_value = cookies[len(cookies) - 1].get("value")
if language_value is not None:
assert_in(LANGUAGE["ENGLISH"], language_value)
assert_not_in(LANGUAGE["GERMAN"], language_value)
else:
raise Exception("Cookie language value is empty")
finally:
driver.close()
def rs3topng(rs3_filepath, png_filepath=None):
"""Convert a RS3 file into a PNG image of the RST tree.
If no output filename is given, the PNG image is returned
as a string (which is useful for embedding).
"""
try:
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
except ImportError:
raise ImportError(
'Please install selenium: pip install selenium')
html_str = rs3tohtml(rs3_filepath)
temp = tempfile.NamedTemporaryFile(suffix='.html', delete=False)
temp.write(html_str.encode('utf8'))
temp.close()
try:
driver = webdriver.PhantomJS()
except WebDriverException as err:
raise WebDriverException(
'Please install phantomjs: http://phantomjs.org/\n' + err.msg)
driver.get(temp.name)
os.unlink(temp.name)
png_str = driver.get_screenshot_as_png()
if png_filepath:
with open(png_filepath, 'w') as png_file:
png_file.write(png_str)
else:
return png_str
def __init__(self, settings):
self.options = settings.get('PHANTOMJS_OPTIONS', {}) # ???
max_run = settings.get('PHANTOMJS_MAXRUN', 10) # PhantomJS ???????????, ??10
self.sem = defer.DeferredSemaphore(max_run)
self.queue = Queue.LifoQueue(maxsize=max_run) # LifoQueue ??????
SignalManager(dispatcher.Any).connect(receiver=self._close, signal=signals.spider_closed)
def _wait_request(self, request, spider):
try:
driver = self.queue.get_nowait()
except:
driver = webdriver.PhantomJS(**self.options)
driver.get(request.url)
# wait until ajax completed
dfd = threads.deferToThread(self._wait_and_switch, driver)
dfd.addCallback(self._response, driver, spider)
return dfd
def get_pages(self):
'''
??Phantomjs??????????????????url
Get all pages' urls using selenium an phantomJS
return:
a list of tuple (page_num,page_url)
'''
r_slt=r'onchange="select_page\(\)">([\s\S]*?)</select>'
r_p=r'<option value="(.*?)".*?>?(\d*?)?<'
try:
dcap = dict(DesiredCapabilities.PHANTOMJS)
# ???????????????
dcap["phantomjs.page.settings.loadImages"] = False
driver = webdriver.PhantomJS(desired_capabilities=dcap)
driver.get(self.chapter_url)
text=driver.page_source
st=re.findall(r_slt,text)[0]
self.pages = [(int(p[-1]),p[0]) for p in re.findall(r_p,st)]
except Exception:
traceback.print_exc()
self.pages = []
except KeyboardInterrupt:
raise KeyboardInterrupt
finally:
driver.quit()
print('Got {l} pages in chapter {ch}'.format(l=len(self.pages),ch=self.chapter_title))
return self.pages
def get_taobao_cate():
url = 'https://shopsearch.taobao.com/search?app=shopsearch'
driver = webdriver.PhantomJS(executable_path="d:\\phantomjs.exe")
driver.get(url)
driver.implicitly_wait(3)
page = driver.page_source
soup = BeautifulSoup(page, 'lxml')
cate_name = re.findall(r"q=(.*?)&tracelog=shopsearchnoqcat", str(soup))
for c in cate_name:
cname = urllib.parse.unquote(c, encoding='gb2312')
cate_list.append(c)
print(cname)
print(cate_list)
def _get_PhantomJS(self):
try:
service_args = []
if self.proxy:
service_args.extend([
'--proxy={}:{}'.format(self.proxy.host, self.proxy.port),
'--proxy-type={}'.format(self.proxy.proto),
])
if self.proxy.username and self.proxy.password:
service_args.append(
'--proxy-auth={}:{}'.format(
self.proxy.username,
self.proxy.password
)
)
useragent = random_user_agent(
mobile=False
)
logger.info('useragent: {}'.format(useragent))
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = useragent
try:
self.webdriver = webdriver.PhantomJS(
executable_path=self.config['executable_path'],
service_args=service_args,
desired_capabilities=dcap
)
return True
except (ConnectionError, ConnectionRefusedError, ConnectionResetError) as err:
logger.error(err)
return False
except WebDriverException as e:
logger.error(e)
return False
def __init__(self):
self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
self.driver = webdriver.PhantomJS()
self.driver.get('http://www.investopedia.com/markets/stocks/tsla/')
self.driver.save_screenshot('screen.png') # save a screenshot to disk
networkActivity = str(re.findall('https:\/\/superquotes\.xignite\.com\/((.*?))"', str(self.driver.get_log('har')))[0])
self.Token = str(networkActivity.partition("&_token=")[2]).partition('&')[0]
self.UserID = ''.join(re.findall('(\d+)\D', str(networkActivity.partition("&_token_userid=")[2].partition(' ')[0])))
def make_browser(cls):
# Build a selenium browser
try:
cls.browser = webdriver.PhantomJS()
except Exception:
try:
# Fall back to Firefox
cls.browser = webdriver.Firefox()
except:
raise Exception("Could not start a Firefox or PhantomJS instance!")
cls.browser.get("http://127.0.0.1:%i/" % cls.port_num)
# Setup to support routing
cls.app = cls._make_app()
def phantom_driver():
return webdriver.PhantomJS(service_args=["--ignore-ssl-errors=true", "--web-security=false"])
def get_title_with_screenshot(url):
driver = webdriver.PhantomJS(service_args = service_args, desired_capabilities = dcap)
driver.set_window_size(1024, 512)
driver.get('http://' + url + '.onion') # 'http://' is required.
driver.save_screenshot(url + '.png')
title = driver.title
driver.close()
return title
def __init__(self):
self.driver = webdriver.PhantomJS(service_args=['--load-images=false', '--disk-cache=true'])
def Launch():
"""
Launch the Medium bot and ask the user what browser they want to use.
"""
if 'chrome' not in DRIVER.lower() and 'firefox' not in DRIVER.lower() and 'phantomjs' not in DRIVER.lower():
# Browser choice
print 'Choose your browser:'
print '[1] Chrome'
print '[2] Firefox/Iceweasel'
print '[3] PhantomJS'
while True:
try:
browserChoice = int(raw_input('Choice? '))
except ValueError:
print 'Invalid choice.',
else:
if browserChoice not in [1,2,3]:
print 'Invalid choice.',
else:
break
StartBrowser(browserChoice)
elif 'chrome' in DRIVER.lower():
StartBrowser(1)
elif 'firefox' in DRIVER.lower():
StartBrowser(2)
elif 'phantomjs' in DRIVER.lower():
StartBrowser(3)