Advanced Options
BrowserQL offers advanced options allows developers to create dynamic, condition-based workflows, capture visual data, and interact programmatically with web pages. These options enhance the flexibility of web automation, making it possible to tackle complex scenarios with ease. Below, you’ll find detailed examples and use cases to help you with these advanced features.
Conditional Behaviors
You can use if
or ifnot
mutations to trigger nested work branches based on specific conditions. A common use case for this is when a browser interaction receives a 403 code, indicating the need to use a verification or captcha-solving mutation.
The example below will only trigger the verify
mutation when the goto
returns a 403 code:
mutation ConditionalCase {
goto(url: "https://www.browserless.io/", waitUntil: domContentLoaded) {
status
}
if(response: {codes: 403}) {
waitForTimeout(time: 2000) {
time
}
verify(type: cloudflare) {
solved
}
}
}
Now, a ifnot
can be used to treat cases where the code isn't 403. For example, the code below retrieves the page's title when any other code is returned:
mutation ConditionalCase {
goto(url: "https://www.browserless.io/", waitUntil: domContentLoaded) {
status
}
if(response: {codes: 403}) {
waitForTimeout(time: 2000) {
time
}
verify(type: cloudflare) {
solved
}
}
ifnot(response: {codes: 403}) {
title {
title
}
}
}
This example opens up two different work branchs, one that will be triggered when a code 403 is received, and a second one to handle when any other code is returned.
Taking Screenshots
BrowserQL can be used to take screenshots from any website. With the screenshot mutator, you can generate any image, and BQL will return a base64 encoded string of the new image.
For example, the code below takes a picture of https://example.com/, focusing on the middle section using the clip
argument to define the image's size, and screen position.
- Screenshot
- With Selector
- Response
mutation TakeScreenshot {
goto(url: "https://example.com/") {
status
}
screenshot(clip: {width: 720, height: 300, x: 150, y: 65}) {
base64
}
}
mutation TakeScreenshot {
goto(url: "https://example.com/") {
status
}
screenshot(selector: "div.logo_footer.w-embed") {
base64
}
}
{
"data": {
"goto": {
"status": 200
},
"screenshot": {
"base64": "iVBORw0KGgoAAAANSUhEUgAAAtAAAAEsCAIAAABl...AAEA6ggMAAEhHcAAAAOkIDgAAIB3BAQAApCM4AACAdAQHAACQjuAAAADS/QfLjprKnsPc9AAAAABJRU5ErkJggg=="
}
}
}
Refer to the Mutation Reference page for full details on the available arguments and response from the screenshot mutation.
Now, with the image's base64, you can use it, as exemplified below, replacing <PASTE_YOUR_BASE64_HERE>
with your base64 string:
- HTML
- Save File
<!DOCTYPE html>
<html>
<head>
<title>Base64 Image Viewer</title>
</head>
<body>
<img src="data:image/png;base64,<PASTE_YOUR_BASE64_HERE>" alt="Base64 Image">
</body>
</html>
import base64
# Base64 string
base64_string = "<PASTE_YOUR_BASE64_HERE>"
# Decode and save as a file
with open("output.png", "wb") as file:
file.write(base64.b64decode(base64_string))
Generating PDFs
Similarly to taking screenshots, there is the pdf
mutation to generate PDFs. BQL will generate a base64 encoded string from any website.
mutation GetPDF {
goto(url: "https://example.com/") {
status
}
pdf {
base64
}
}
Refer to the Mutation Reference page for full details on the available arguments and response from the pdf mutation.
With the PDF's base64 string, you can generate the PDF file, or make it downloadable in your own site.
- HTML
- Save File
<!DOCTYPE html>
<html>
<head>
<title>Base64 PDF Viewer</title>
</head>
<body>
<iframe src="data:application/pdf;base64,<PASTE_YOUR_BASE64_HERE>" width="600" height="400"></iframe>
<a href="data:application/pdf;base64,<PASTE_YOUR_BASE64_HERE>" download="document.pdf">Download PDF</a>
</body>
</html>
import base64
# Base64 string
base64_string = "<PASTE_YOUR_BASE64_HERE>"
# Decode and save as a file
with open("output_document.pdf", "wb") as file:
file.write(base64.b64decode(base64_string))