Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/oxylabs/playwright-proxy-integration-js

A tutorial for implementing Oxylabs` Residential and Datacenter proxies with Playwright using JavaScript
https://github.com/oxylabs/playwright-proxy-integration-js

javascript nodejs playwright playwright-java proxies proxy-list proxy-list-github proxy-rotator proxy-site residential-proxy rotating-proxy web-scraping

Last synced: about 2 months ago
JSON representation

A tutorial for implementing Oxylabs` Residential and Datacenter proxies with Playwright using JavaScript

Awesome Lists containing this project

README

        

# Oxylabs' Proxy Integration with Playwright

[![Oxylabs promo code](https://user-images.githubusercontent.com/129506779/250792357-8289e25e-9c36-4dc0-a5e2-2706db797bb5.png)](https://oxylabs.go2cloud.org/aff_c?offer_id=7&aff_id=877&url_id=112)

[![](https://dcbadge.vercel.app/api/server/eWsVUJrnG5)](https://discord.gg/GbxmdGhZjq)

[](https://github.com/topics/javascript) [](https://github.com/topics/web-scraping) [](https://github.com/topics/rotating-proxies)
- [Requirements](#requirements)
- [Integrating Datacenter Proxies](#integrating-datacenter-proxies)
- [Integrating Residential Proxies](#integrating-residential-proxies)
## Requirements

### Playwright
```bash
npm install playwright
```

## Integrating Datacenter Proxies

### Getting a List of Proxies

Open the following URL in the browser and enter your credentials. You will see a list of proxies in plain text:

```
https://proxy.oxylabs.io/all
```

### Using Proxies

If you wish to select any of the provided proxies, you need to save the proxy IP, along with the port, in a variable.

To use all these proxies, first, save these proxies as an array in your code:

```javascript
let proxies = [
'127.0.0.1:60000',
'127.0.0.2:60000',
'127.0.0.3:60000',
'127.0.0.4:60000'
]
```

To select one of the proxies randomly, use the following line of code:

```JavaScript
var proxy = proxies[Math.floor(Math.random() * proxies.length)];
```

Create a variable called `launchOptions` and assign the proxy information to it.

Don't forget to replace `USERNAME` and `PASSWORD` with your proxy user credentials.

```javascript
const launchOptions = {
proxy: {
server: proxy,
username: 'USERNAME',
password: 'PASSWORD'
}
};
```

After creating the `launchOptions` variable, create a `playwright` instance and launch the browser.

```javascript
const browser = await chromium.launch(launchOptions);
```

For the complete code sample, see [this file](datacenter_random.js).

## Integrating Residential Proxies

### Random Proxy Using the Proxy API
Proxy information can be supplied as a parameter to the `launch` method of `playwright` instance.

Alternatively, create a variable called `launchOptions` and assign the proxy information to it.

Don't forget to replace `USERNAME` and `PASSWORD` with your proxy user credentials.

```javascript
const launchOptions = {
proxy: {
server: 'pr.oxylabs.io:7777',
username: 'USERNAME',
password: 'PASSWORD'
},
headless: false
};
```

The additional benefit of using lauchOptions variable is that other information such as `headless` can be supplied to the `launch` method.:

After creating the `launchOptions` variable, create a `playwright` instance and launch the browser.

```javascript
const browser = await chromium.launch(launchOptions);
```

For the complete code sample, see [this file](residential_random.js).

### Country Specific Proxies

If you wish to use country-specific proxies, all you need to do is change the proxy server.

For example, if you wish to use the proxy for the United States, you can use the following code:

```javascript
const launchOptions = {
proxy: {
server: 'us-pr.oxylabs.io:10001',
username: 'USERNAME',
password: 'PASSWORD'
},
headless: false
};

```

In this example, us-pr.oxylabs.io:10000 is the country specific entry point for the United States.

Another example is `gb-pr.oxylabs.io:20000`, which is the country specific entry point for United Kingdom.

For a complete list of all entry points, see [Country Specific Entry Nodes](https://oxy.yt/zrG0).