Skip to content

Commit 03510d9

Browse files
authored
Add files via upload
1 parent 4df2d61 commit 03510d9

File tree

4 files changed

+250
-2
lines changed

4 files changed

+250
-2
lines changed

README.md

+120-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,120 @@
1-
# playwright-proxy-integration-js
2-
A tutorial for implementing Oxylabs
1+
# Oxylabs' Proxy Integration with Playwright
2+
3+
[<img src="https://img.shields.io/static/v1?label=&message=JavaScript&color=brightgreen" />](https://github.com/topics/python) [<img src="https://img.shields.io/static/v1?label=&message=Web%20Scraping&color=important" />](https://github.com/topics/web-scraping) [<img src="https://img.shields.io/static/v1?label=&message=Rotating%20Proxies&color=blueviolet" />](https://github.com/topics/rotating-proxies)
4+
- [Requirements](#requirements)
5+
- [Integrating Datacenter Proxies](#integrating-datacenter-proxies)
6+
- [Integrating Residential Proxies](#integrating-residential-proxies)
7+
## Requirements
8+
9+
### Playwright
10+
```bash
11+
npm install playwright
12+
```
13+
14+
## Integrating Datacenter Proxies
15+
16+
### Getting a List of Proxies
17+
18+
Open the following URL in the browser, enter your credentials, and you will see a list of proxies in plain text:
19+
20+
```
21+
https://proxy.oxylabs.io/all
22+
```
23+
24+
### Using Proxies
25+
26+
If you wish to select any one of the proxies, save the proxy IP, along with the port in a variable.
27+
28+
To use all these proxies, first, save these proxies as an array in your code:
29+
30+
```javascript
31+
let proxies = [
32+
'127.0.0.1:60000',
33+
'127.0.0.2:60000',
34+
'127.0.0.3:60000',
35+
'127.0.0.4:60000'
36+
]
37+
```
38+
39+
To select one of these randomly, use the following line of code:
40+
41+
```JavaScript
42+
var proxy = proxies[Math.floor(Math.random() * proxies.length)];
43+
```
44+
45+
create a variable called `launchOptions` and assign the proxy information to it.
46+
47+
Don't forget to replace `USERNAME` and `PASSWORD` with your proxy user credentials.
48+
49+
```javascript
50+
const launchOptions = {
51+
proxy: {
52+
server: proxy,
53+
username: 'USERNAME',
54+
password: 'PASSWORD'
55+
}
56+
};
57+
```
58+
59+
After creating the `launchOptions` variable, create a `playwright` instance and launch the browser.
60+
61+
```javascript
62+
const browser = await chromium.launch(launchOptions);
63+
```
64+
65+
For the complete code sample, see [this file](datacenter_random.js).
66+
67+
68+
## Integrating Residential Proxies
69+
70+
### Random Proxy Using the Proxy API
71+
Proxy information can be supplied as a parameter to the `launch` method of `playwright` instance.
72+
73+
Alternatively, create a variable called `launchOptions` and assign the proxy information to it.
74+
75+
Don't forget to replace `USERNAME` and `PASSWORD` with your proxy user credentials.
76+
77+
```javascript
78+
const launchOptions = {
79+
proxy: {
80+
server: 'pr.oxylabs.io:7777',
81+
username: 'USERNAME',
82+
password: 'PASSWORD'
83+
},
84+
headless: false
85+
};
86+
```
87+
88+
The additional benefit of using lauchOptions variable is that other information such as `headless` can be supplied to the `launch` method.:
89+
90+
After creating the `launchOptions` variable, create a `playwright` instance and launch the browser.
91+
92+
```javascript
93+
const browser = await chromium.launch(launchOptions);
94+
```
95+
96+
For the complete code sample, see [this file](residential_random.js).
97+
98+
### Country Specific Proxies
99+
100+
If you wish to use country-specific proxies, all you need to do is to change the `proxy` `server`.
101+
102+
For example, if you wish to use the proxy for the United States, you can use the following code:
103+
104+
```javascript
105+
const launchOptions = {
106+
proxy: {
107+
server: 'us-pr.oxylabs.io:10001',
108+
username: 'USERNAME',
109+
password: 'PASSWORD'
110+
},
111+
headless: false
112+
};
113+
114+
```
115+
116+
In this example, `us-pr.oxylabs.io:10000` is the country specific entry point for United States.
117+
118+
Another example is `gb-pr.oxylabs.io:20000`, which is the country specific entry point for United Kingdom.
119+
120+
For a complete list of all entry points, see [Country Specific Entry Nodes](https://developers.oxylabs.io/residential-proxies/#country-specific-entry-nodes).

datacenter_random.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Import Chromium from Playwright
2+
const { chromium } = require('playwright');
3+
4+
// Array that contains the proxy servers
5+
let proxies = [
6+
'20.94.229.106:80',
7+
'209.141.55.228:80',
8+
'103.149.162.194:80',
9+
'206.253.164.122:80'
10+
];
11+
12+
// Create an anonymous and asynchronous function and call it immediately
13+
(async () => {
14+
15+
// Randomly select a proxy from the array
16+
var proxy = proxies[Math.floor(Math.random() * proxies.length)];
17+
18+
// Create a variable for launch options
19+
// Set the proxy server to the proxy variable
20+
// Set the username and password provided by Oxylabs
21+
const launchOptions = {
22+
proxy: {
23+
24+
// Proxy server and credentials
25+
server: proxy,
26+
username: 'USERNAME',
27+
password: 'PASSWORD'
28+
},
29+
30+
// Set additional launch options here
31+
headless: false
32+
};
33+
34+
// Lauch the browser with the launch options
35+
const browser = await chromium.launch(launchOptions);
36+
37+
// Use a try-catch-finally to ensure that browser is closed
38+
try {
39+
// Create a new page
40+
const page = await browser.newPage();
41+
42+
// This page simply returns the IP address
43+
await page.goto('https://httpbin.org/ip');
44+
45+
// Print the response from the page
46+
// This will print the IP address of the proxy
47+
console.log(await page.textContent("*"));
48+
} catch (e) {
49+
// print the error to the console
50+
console.log(e);
51+
}
52+
finally {
53+
// Close the browser
54+
await browser.close();
55+
56+
}
57+
58+
})();

package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "playwright-proxy",
3+
"version": "1.0.0",
4+
"description": "- [Requirements](#requirements) - [Integrating Datacenter Proxies](#integrating-datacenter-proxies) - [Integrating Residential Proxies](#integrating-residential-proxies) ## Requirements",
5+
"main": "_single proxy.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/WorkOB/Oxylabs-Proxy-Integration-with-Playwright.git"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "ISC",
16+
"bugs": {
17+
"url": "https://github.com/WorkOB/Oxylabs-Proxy-Integration-with-Playwright/issues"
18+
},
19+
"homepage": "https://github.com/WorkOB/Oxylabs-Proxy-Integration-with-Playwright#readme",
20+
"dependencies": {
21+
"playwright": "^1.17.2"
22+
}
23+
}

residential_random.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Import Chromium from Playwright
2+
const { chromium } = require('playwright');
3+
4+
// Create an anonymous and asynchronous function and call it immediately
5+
(async () => {
6+
// For residential proxy, set the API endpoint as the proxy server
7+
// For country-specific proxy, use different API endpoint
8+
// See https://developers.oxylabs.io/residential-proxies/#random-proxy-entry-nodes
9+
var proxy = 'pr.oxylabs.io:7777';
10+
11+
// Create a variable for launch options
12+
// Set the proxy server to the server variable
13+
// Set the username and password provided by Oxylabs
14+
const launchOptions = {
15+
16+
// Proxy server and credentials
17+
proxy: {
18+
server: proxy,
19+
username: 'USERNAME',
20+
password: 'PASSWORD'
21+
},
22+
// Set additional launch options here
23+
headless: false
24+
};
25+
26+
// Lauch the browser with the launch options
27+
const browser = await chromium.launch(launchOptions);
28+
29+
// Use a try-catch-finally to ensure that browser is closed
30+
try {
31+
// Create a new page
32+
const page = await browser.newPage();
33+
34+
// This page simply returns the IP address
35+
await page.goto('https://ip.oxylabs.io/');
36+
37+
// Print the response from the page
38+
// This will print the IP address of the proxy
39+
console.log(await page.textContent("*"));
40+
} catch (e) {
41+
// print the error to the console
42+
console.log(e);
43+
}
44+
finally {
45+
// Close the browser
46+
await browser.close();
47+
}
48+
49+
})();

0 commit comments

Comments
 (0)