首页 > 解决方案 > 在 Selenium 版本 3.7.0 无头驱动程序 phantomjs 版本 2.1 上启用 Cookie

问题描述

我正在尝试selenium使用已弃用的无头驱动程序运行,phantomjs因为它在无头模式下接受经过身份验证的代理。

我正在尝试登录亚马逊网站,但无法点击continue按钮。

加载页面源状态我必须启用 cookie:

<div id="auth-cookie-warning-message" class="a-box a-alert a-alert-warning"><div class="a-box-inner a-alert-container"><h4 class="a-alert-heading">Please Enable Cookies to Continue</h4><i class="a-icon a-icon-alert"></i><div class="a-alert-content">

我的代码如下:

headers = { 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:63.0) Gecko/20100101 Firefox/63.0",
'Connection': 'keep-alive'
}
for key, value in headers.items():
    webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.{}'.format(key)] = value
webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.userAgent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:63.0) Gecko/20100101 Firefox/63.0"
CHROMEDRIVER_PATH = './phantomjs-2.1.1-linux-x86_64/bin/phantomjs'
WINDOW_SIZE = "1920,1080"

proxy = '45.95.99.20:7580'

service_args=['--ignore-ssl-errors=true',
    '--ssl-protocol=any',
    '--proxy={}'.format(proxy),
    '--proxy-type=http',
    '--proxy-auth={}:{}'.format(proxy_user, proxy_pass)]
service_args.append('--ignore-ssl-errors=true')
service_args.append('--web-security=no')
driver = webdriver.PhantomJS(
    executable_path=CHROMEDRIVER_PATH,
    service_args=service_args
    )
driver.get(url)
username_input = driver.find_element_by_css_selector("input[name='email']")
username_input.send_keys(login)

login_button=driver.find_element_by_id('continue')
login_button.click()

设置如下

Phantomjs 2.1
Selenium 3.7.0
Ubuntu 18.04

单击 时continue,没有任何反应。

我该如何克服这个问题并启用 cookie?

在此处输入图像描述

标签: pythonseleniumproxyphantomjsheadless-browser

解决方案


通过使用webdriver.ActionChains()insted of解决了它click()

action = webdriver.ActionChains(driver)
login_button=driver.find_element_by_id('continue')
action.move_to_element(login_button).click().perform()

推荐阅读