Setting referer in Selenium

sam picture sam · Dec 22, 2013 · Viewed 15.1k times · Source

Im working with the selenium remote driver to automate actions on a site, i can open the page i need directly by engineering the url as the sites url schema is very constant. This speeds up the script as it dose not have to work through several pages before it gets to the one it needs.

To make the automation seem organic is there a way to set a referral page in Selenium ?

Answer

Rebs picture Rebs · Feb 20, 2017

If you're checking the referrer on the server, then using a proxy (as mentioned in other answers) will be the way to go.

However, if you need access to the referrer in Javascript using a proxy will not work. To set the Javascript referrer I did the following:

  1. Go to the referral website
  2. Inject this javascript onto the page via Selenium API: document.write('<script>window.location.href = "<my website>";</script>')"

I'm using a Python wrapper around selenium, so I cannot provide the function you need to inject the code in your language, but it should be easy to find.