def get_data(): driver = Driver.phantomjs() driver.command_executor._commands['executePhantomScript'] = ( 'POST', '/session/$sessionId/phantom/execute') script = """ var page = this; //page.onResourceReceived = function(response) { // console.log('response: ' + JSON.stringify(response)); // console.log('(#' + response.id + ', stage "' + response.stage + '": ' + response.url + ' ' + JSON.stringify(response.headers)); //}; page.onResourceRequested = function (requestData, request) { if (requestData.method == 'POST'){ console.log('method, url: ' + requestData.method + ', ' + requestData.url); console.log('Request ' + JSON.stringify(request, undefined, 4)); } }; """.strip() # check ghostdriver.log file for the result of the statement below driver.execute('executePhantomScript', {'script': script, 'args': []}) try: driver.get("https://radar.techcabal.com/") time.sleep( 5 ) # allows for proper load on some either slow network or resource intensive webpage driver.save_screenshot("radar-tech-cabal.png") except WebDriverException, e: driver.save_screenshot("radar-tech-cabal-exception.png") print("WebDriverException: {} {}".format(e, sys.exc_info()[0]))
def test_chrome(self): try: self.driver = Driver.chrome() self.driver.get("https://app.simplegoods.co/i/IQCZADOY" ) # url associated with button click button = self.driver.find_element_by_id( "payment-submit").get_attribute("value") self.assertEquals(u'Pay - $60.00', button) except WebDriverException: pass except (Exception, ): pass
def get_data(): """ Aborts .css files and adverts linking to some pages :return: """ driver = Driver.phantomjs(image=True) driver.command_executor._commands['executePhantomScript'] = ( 'POST', '/session/$sessionId/phantom/execute') script = """ var page = this; // being the actual size of the headless browser // page.viewportSize = { width: 1440, height: 900 }; var title = page.evaluate(function () { return document.title; }); var clipRect = page.evaluate(function(selector){ // document.body.style.backgroundColor = "#000000"; return document.querySelector(selector).getBoundingClientRect(); }, '#sponsors'); // page.clipRect = {top: 0, left: 0, width: 200, height: 400}; page.clipRect = { top: clipRect.top, left: clipRect.left, width: clipRect.width, height: clipRect.height }; output_file = new Date().getTime() + ".png" page.render(output_file, {format: 'png'}); // page.render(new Date().getTime() + ".png"); """ try: driver.get("https://pycon.ng") time.sleep( 5 ) # allows for proper load on some either slow network or resource intensive webpage driver.save_screenshot("pycon.png") # save a snapshot # check ghostdriver.log file for the result of the statement below driver.execute('executePhantomScript', {'script': script, 'args': []}) except WebDriverException, e: driver.save_screenshot("pycon-exception.png") print("WebDriverException: {} {}".format(e, sys.exc_info()[0]))
def stringify(intake): """ Takes a valid JSON as input and emits it in valid string format :param intake: the valid string. :return: a valid JSON string format. Else, the exception message is returned """ driver = Driver.phantomjs( ) # Driver.firefox(), Driver.chrome(headless=False) try: driver.get(url="file:///") # return driver.execute_script('return JSON.stringify({0}) || null;'.format(intake)) return driver.execute_script( 'return JSON.stringify({0}, undefined, 4) || null;'.format(intake)) except WebDriverException, e: return "WebDriverException: {} {}".format(e, sys.exc_info()[0])
def stringify(intake): """ Takes a valid JSON as input and emits it in valid string format PhantomJS in this case will not emit a log file :param intake: the valid string. :return: a valid JSON string format. Else, the exception message is returned """ driver = Driver.phantomjs(filename=os.path.devnull) try: driver.get(url="file:///") # return driver.execute_script('return JSON.stringify({0}) || null;'.format(intake)) return driver.execute_script( 'return JSON.stringify({0}, undefined, 4) || null;'.format(intake)) except WebDriverException, e: return "WebDriverException: {} {}".format(e, sys.exc_info()[0])
def get_data(): """ Aborts .css files and adverts linking to some pages :return: """ driver = Driver.phantomjs() driver.command_executor._commands['executePhantomScript'] = ( 'POST', '/session/$sessionId/phantom/execute') script = """ var page = this; var skip = ['googleads.g.doubleclick.net', 'cm.g.doubleclick.net', 'www.googleadservices.com']; skip.push.apply(skip, ['googlesyndication.com', 'doubleclick.net', 'google-analytics.com']); page.onResourceRequested = function (requestData, networkRequest) { if (requestData.url.indexOf(".css") > -1) { console.log('Request (#' + requestData.id + '): ' + requestData.url + ' -> abort'); networkRequest.abort(); } skip.forEach(function (needle) { if (requestData.url.indexOf(needle) > 0) { networkRequest.abort(); } }); // if (/googlesyndication.com|doubleclick.net|google-analytics.com/.test(requestData.url)) { // console.log('Request (#' + requestData.id + '): ' + requestData.url + ' -> abort'); // networkRequest.abort(); // } }; """.strip() # check ghostdriver.log file for the result of the statement below driver.execute('executePhantomScript', {'script': script, 'args': []}) try: driver.get("https://radar.techcabal.com/") time.sleep( 5 ) # allows for proper load on some either slow network or resource intensive webpage driver.save_screenshot("radar-tech-cabal.png") except WebDriverException, e: driver.save_screenshot("radar-tech-cabal-exception.png") print("WebDriverException: {} {}".format(e, sys.exc_info()[0]))
def tell_time(): """ Shows the time in GMT :return: a string. Else, the exception message is returned """ driver = Driver.phantomjs() intake = """ var now = new Date(); return JSON.stringify({"gmt_time":now, "epoch":now.getTime()}, undefined, 4) || null; """.strip() try: driver.get(url="file:///") return driver.execute_script("{0}".format(intake)) except WebDriverException, e: return "WebDriverException: {} {}".format(e, sys.exc_info()[0])
def setUp(self): self.driver = Driver.phantomjs()