Not able to take full page screenhot in selinium web driver- firefox - node.js

Hey what is the command to take full screen screenshot in selinium webdriver nodejs.
here is my code:
var webdriver = require('selenium-webdriver');
By = require('selenium-webdriver').By;
until = require('selenium-webdriver').untill;
fs = require('fs');
var chromedriver = require('chromedriver');
firefox = require('selenium-webdriver/firefox');
var Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities;
var capabilities = Capabilities.firefox();
capabilities.set('marionette', true);
//driver = new FirefoxDriver();
var driver = new webdriver.Builder().withCapabilities(capabilities).build();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.get('http://iolearn.com');
driver.takeScreenshot().then(function(data){
fs.writeFileSync('img.png',data,'base64');
});
driver.quit();

Related

Experimental Chrome Options in Selenium Node.js

I need to convert these python lines:
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(executable_path='<path-to-chrome>', options=options)
From this issue: Python selenium: DevTools listening on ws://127.0.0.1
I don't know how to add experimental options in Node.js, I can't find any documentation.
I also couldn't find the experimental chrome options in Selenium Node.js. But if you want to exclude a chrome switch I think you can do it like this:
const {Builder} = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome');
const chromeOptions = new chrome.Options()
chromeOptions.excludeSwitches("enable-logging")
let driver = await new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();

Display vega spec in Jupyter Lab

How can I display a vega spec (such as this force directed layout) in Jupyter Lab/JupyterLab?
If you don't mind installing altair, you can do this:
from altair.vega import vega
import json
with open("bar-chart.vg.json") as f:
s = json.load(f)
vega(s)
Alternatively, you can use Vega Embed through the javascript extension:
Add the scripts:
%%javascript
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '//cdn.jsdelivr.net/npm/vega#5';
document.head.appendChild(script);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '//cdn.jsdelivr.net/npm/vega-embed#6';
document.head.appendChild(script);
then
from IPython.display import Javascript
script = '''
var spec = "https://raw.githubusercontent.com/vega/vega/master/docs/examples/bar-chart.vg.json";
vegaEmbed(element, spec).then(function(result) {
}).catch(console.error);
'''
Javascript(script)
Note: The example force directed spec at https://raw.githubusercontent.com/vega/vega/master/docs/examples/force-directed-layout.vg.json doesn't display because it references data at a relative url (data/miserables.json). The bar chart works because the data is encoded directly into the spec.

Capture logs from Chrome during test is running. Python

I'm trying to capture logs from chrome, during automated test is running. Code is below:
binary_path = 'Chromium'
chromedriver_path = 'chromedriver'
opts = Options()
opts.binary_location = binary_path
d = DesiredCapabilities.CHROME
d['goog:loggingPrefs'] = { 'browser':'ALL',
'driver': 'ALL'}
driver = webdriver.Chrome(chromedriver_path, desired_capabilities=d, options=opts)
#EXAMPLE TO GET LOGS
driver.get('https://www.google.com/')
driver.find_element_by_name('q').send_keys('abc')
print('browser = ', driver.get_log('browser'))
print('driver = ', driver.get_log('driver'))
for entry in driver.get_log('browser'):
print('entry = ', entry)
driver.quit()
output from print:
browser = []
driver = []
My question is, why entry isn't printing and why there is no output from chrome logs
Try using ChromeOptions instead of Options.
Also, not sure why you use Chromium, this might affect the result.
I modified your code and following code worked for me with Chrome browser:
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver import ChromeOptions
opts = ChromeOptions()
d = DesiredCapabilities.CHROME
d['goog:loggingPrefs'] = {'browser': 'ALL',
'driver': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=d, options=opts)
#EXAMPLE TO GET LOGS
driver.get('https://www.google.com/')
driver.find_element_by_name('q').send_keys('abc')
print('browser = ', driver.get_log('browser'))
print('driver = ', driver.get_log('driver'))
for entry in driver.get_log('browser'):
print('entry = ', entry)
driver.quit()
it printed out =>
browser = []
driver = [{'level': 'INFO', 'message': 'Populating Preferences file: {\n "alternate_error_pages": {\n "enabled": false\n },\n "autofill": {\n "enabled": false\n }, ... ]
I hope this helps, good luck!

How can we use microphone in google colab?

OSError Traceback (most recent call last)
<ipython-input-21-4159a88154c9> in <module>()
7 response = google_images_download.googleimagesdownload()
8 r = sr.Recognizer()
----> 9 with sr.Microphone() as source:
10 print("Say something!")
11 audio = r.listen(source)
/usr/local/lib/python3.6/dist-packages/speech_recognition/init.py in init(self, device_index, sample_rate, chunk_size)
84 assert 0 <= device_index < count, "Device index out of range ({} devices available; device index should be between 0 and {} inclusive)".format(count, count - 1)
85 if sample_rate is None: # automatically set the sample rate to the hardware's default sample rate if not specified
---> 86 device_info = audio.get_device_inf o_by_index(device_index) if device_index is not None else audio.get_default_input_device_info()
87 assert isinstance(device_info.get("defaultSampleRate"), (float, int)) and device_info["defaultSampleRate"] > 0, "Invalid device info returned from PyAudio: {}".format(device_info)
88 sample_rate = int(device_info["defaultSampleRate"])
Here's an example that shows how to access a user's camera and microphone:
https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=2viqYx97hPMi
The snippet you linked above attempts to access a microphone in Python. That won't work because there's no microphone attached to the virtual machine which executes Python code in Colab.
Instead, you want to access the microphone of the computer running the web browser. Then, capture data there, and pass it back to the virtual machine for processing in Python.
That's what's shown in the snippet linked above.
Here is a simple snippet
from IPython.display import HTML, Audio
from google.colab.output import eval_js
from base64 import b64decode
import numpy as np
from scipy.io.wavfile import read as wav_read
import io
import ffmpeg
AUDIO_HTML = """
<script>
var my_div = document.createElement("DIV");
var my_p = document.createElement("P");
var my_btn = document.createElement("BUTTON");
var t = document.createTextNode("Press to start recording");
my_btn.appendChild(t);
//my_p.appendChild(my_btn);
my_div.appendChild(my_btn);
document.body.appendChild(my_div);
var base64data = 0;
var reader;
var recorder, gumStream;
var recordButton = my_btn;
var handleSuccess = function(stream) {
gumStream = stream;
var options = {
//bitsPerSecond: 8000, //chrome seems to ignore, always 48k
mimeType : 'audio/webm;codecs=opus'
//mimeType : 'audio/webm;codecs=pcm'
};
//recorder = new MediaRecorder(stream, options);
recorder = new MediaRecorder(stream);
recorder.ondataavailable = function(e) {
var url = URL.createObjectURL(e.data);
var preview = document.createElement('audio');
preview.controls = true;
preview.src = url;
document.body.appendChild(preview);
reader = new FileReader();
reader.readAsDataURL(e.data);
reader.onloadend = function() {
base64data = reader.result;
//console.log("Inside FileReader:" + base64data);
}
};
recorder.start();
};
recordButton.innerText = "Recording... press to stop";
navigator.mediaDevices.getUserMedia({audio: true}).then(handleSuccess);
function toggleRecording() {
if (recorder && recorder.state == "recording") {
recorder.stop();
gumStream.getAudioTracks()[0].stop();
recordButton.innerText = "Saving the recording... pls wait!"
}
}
// https://stackoverflow.com/a/951057
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
var data = new Promise(resolve=>{
//recordButton.addEventListener("click", toggleRecording);
recordButton.onclick = ()=>{
toggleRecording()
sleep(2000).then(() => {
// wait 2000ms for the data to be available...
// ideally this should use something like await...
//console.log("Inside data:" + base64data)
resolve(base64data.toString())
});
}
});
</script>
"""
def get_audio():
display(HTML(AUDIO_HTML))
data = eval_js("data")
binary = b64decode(data.split(',')[1])
process = (ffmpeg
.input('pipe:0')
.output('pipe:1', format='wav')
.run_async(pipe_stdin=True, pipe_stdout=True, pipe_stderr=True, quiet=True, overwrite_output=True)
)
output, err = process.communicate(input=binary)
riff_chunk_size = len(output) - 8
# Break up the chunk size into four bytes, held in b.
q = riff_chunk_size
b = []
for i in range(4):
q, r = divmod(q, 256)
b.append(r)
# Replace bytes 4:8 in proc.stdout with the actual size of the RIFF chunk.
riff = output[:4] + bytes(b) + output[8:]
sr, audio = wav_read(io.BytesIO(riff))
return audio, sr
then run this
audio, sr = get_audio()
you might need to install this one
!pip install ffmpeg-python

How to download the PDF by using Selenium Module (FireFox) in Python 3

I want to download a PDF which is from an Online-Magazin. In order to open it, must log in first. Then open the PDF and download it.
The following is my code. It can login to the page and the PDF can also be open. But the PDF can not be downloaded since I am not sure how to simulate the click on Save. I use FireFox.
import os, time
from selenium import webdriver
from bs4 import BeautifulSoup
# Use firefox dowmloader to get file
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", 'D:/eBooks/Stocks_andCommodities/2008/Jul/')
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
fp.set_preference("pdfjs.disabled", "true")
# disable Adobe Acrobat PDF preview plugin
fp.set_preference("plugin.scan.plid.all", "false")
fp.set_preference("plugin.scan.Acrobat", "99.0")
browser = webdriver.Firefox(firefox_profile=fp)
# Get the login web page
web_url = 'http://technical.traders.com/sub/sublogin2.asp'
browser.get(web_url)
# SImulate the authentication
user_name = browser.find_element_by_css_selector('#SubID > input[type="text"]')
user_name.send_keys("thomas2003#test.net")
password = browser.find_element_by_css_selector('#SubName > input[type="text"]')
password.send_keys("LastName")
time.sleep(2)
submit = browser.find_element_by_css_selector('#SubButton > input[type="submit"]')
submit.click()
time.sleep(2)
# Open the PDF for downloading
url = 'http://technical.traders.com/archive/articlefinal.asp?file=\V26\C07\\131INTR.pdf'
browser.get(url)
time.sleep(10)
# How to simulate the Clicking to Save/Download the PDF here?
You should not open the file in browser. Once you have the file url. Get a request session with all the cookies
def get_request_session(driver):
import requests
session = requests.Session()
for cookie in driver.get_cookies():
session.cookies.set(cookie['name'], cookie['value'])
return session
Once you have the session you can download the file using the same
url = 'http://technical.traders.com/archive/articlefinal.asp?file=\V26\C07\\131INTR.pdf'
session = get_request_session(driver)
r = session.get(url, stream=True)
chunk_size = 2000
with open('/tmp/mypdf.pdf', 'wb') as file:
for chunk in r.iter_content(chunk_size):
file.write(chunk)
Apart from Tarun's solution, you can also download the file through js and store it as a blob. Then you can extract the data into python via selinium's execute script as shown in this answer.
In you case,
url = 'http://technical.traders.com/archive/articlefinal.asp?file=\V26\C07\\131INTR.pdf'
browser.execute_script("""
window.file_contents = null;
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
window.file_contents = reader.result;
};
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', %(download_url)s);
xhr.send();
""".replace('\r\n', ' ').replace('\r', ' ').replace('\n', ' ') % {
'download_url': json.dumps(url),
})
Now your data exists as a blob on the window object, so you can easily extract into python:
time.sleep(3)
downloaded_file = driver.execute_script("return (window.file_contents !== null ? window.file_contents.split(',')[1] : null);")
with open('/Users/Chetan/Desktop/dummy.pdf', 'wb') as f:
f.write(base64.b64decode(downloaded_file))
Try
import urllib
file_path = "<FILE PATH TO SAVE>"
urllib.urlretrieve(<pdf_link>,file_path)

Resources