Proxy auth with PhantomJS Proxy auth with PhantomJS selenium selenium

Proxy auth with PhantomJS


Here is the solution I have that works. I ended up needing to pass the credentials in both the service_args & as a proxy-auth header.

service_args = [    "--ignore-ssl-errors=true",    "--ssl-protocol=any",    "--proxy={}".format(proxy),    "--proxy-type=http",]caps = DesiredCapabilities.PHANTOMJSauthentication_token = "Basic " + base64.b64encode(b'{}:{}'.format(username, password))caps['phantomjs.page.customHeaders.Proxy-Authorization'] = authentication_tokenself.driver = webdriver.PhantomJS(        service_args=service_args,        desired_capabilities=caps,        executable_path="./phantomjs-2.1.1-linux-x86_64/bin/phantomjs")

Where proxy's structure is defined as http://username:password@domain:port

I don't know why you have to repeat the credentials. I'd hazard a guess that the first auth-parameters aren't passed as a header to the proxy, so you need to do both manually.


You have to use WebDriverWait to wait for the driver to load the website, once it's done you print the source code.

from selenium.webdriver.support.ui import WebDriverWaitservice_args = [         '--proxy=http://us-ny.proxymesh.com:31280',         '--proxy-type=http', ] authentication_token = "Basic " + base64.b64encode(b'username:pass') capa = DesiredCapabilities.PHANTOMJS capa['phantomjs.page.customHeaders.Proxy-Authorization'] = authentication_token driver = webdriver.PhantomJS( desired_capabilities=capa, service_args=service_args) driver.get(request.url) WebDriverWait(driver, *).until(lambda driver: driver.find_element_by_xpath(*) body = driver.page_source print body

Fill the * with seconds, xpath of the element you want to wait for.