在我的项目中,为了能够访问测试环境网站,我需要发送一个标头请求,否则我会收到 404 错误。我在服务器上使用 Jenkins 运行 Selenium-wire,并在 AWS Device Farm 上运行浏览器。问题是,有些网站不需要标头,我可以正常访问,对于那些网站,下一个配置工作正常:
if browser_name == "chrome":
options = webdriver.ChromeOptions()
options.add_argument("--ignore-certificate-errors")
options.add_argument("--no-sandbox")
devicefarm_client = boto3.client("devicefarm", region_name="us-west-2")
testgrid_url_response = devicefarm_client.create_test_grid_url(
projectArn="arn:aws:devicefarm:us-west-2:111122223333:testgrid-project:123e4567-e89b-12d3-a456-426655440000", # < exemple project's Amazon Resource Name (ARN)
expiresInSeconds=300,
)
desired_capabilities = DesiredCapabilities.CHROME
desired_capabilities["platform"] = "windows"
driver = webdriver.Remote(
testgrid_url_response["url"], desired_capabilities, options=options,
seleniumwire_options={'auto_config': False, 'addr': '127.0.0.1'}
)
driver.set_window_size(1920, 1080)
driver.implicitly_wait(30)
driver.get("...")
对于需要使用标头的站点,我首先开始添加“拦截器”功能,然后尝试了许多其他操作:
elif browser_name == "chrome_1":
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--proxy-server="IP-of-the-machine-running-Jenkins":8087')
devicefarm_client = boto3.client("devicefarm", region_name="us-west-2")
testgrid_url_response = devicefarm_client.create_test_grid_url(
projectArn="arn:aws:devicefarm:us-west-2:111122223333:testgrid-project:123e4567-e89b-12d3-a456-426655440000", # < exemple project's Amazon Resource Name (ARN)
expiresInSeconds=300,
)
desired_capabilities = DesiredCapabilities.CHROME
desired_capabilities["platform"] = "windows"
driver = webdriver.Remote(
testgrid_url_response["url"], desired_capabilities, options=chrome_options,
seleniumwire_options={'auto_config': False, 'addr': '127.0.0.1', 'port': 8087} # < Here I've tried 0.0.0.0, IP of Jenkins machine, etc...
)
driver.set_window_size(1920, 1080)
driver.implicitly_wait(30)
def interceptor(request):
request.headers['x-abc-abcdef'] = 'the-header-value'
driver.request_interceptor = interceptor
driver.get("...")
另外,在本地,带有标头的“拦截器”功能在授予我访问权限方面工作得很好。
如果有人可以在这里提出一些建议,我将不胜感激!
谢谢!