我在 OS X 上使用 selenium webdriver 编写了一个 python 2.7 脚本来登录 Yahoo fantasy sports 并自动执行一些操作。
该脚本适用于 webDriver Firefox 和 Chromedriver。我最近开始使用 PhantomJS (GhostDriver),我发现我无法使用 PhantomJS Selenium Driver (GhostDriver) 登录Yahoo 登录表单。
#!/usr/bin/python
import time
from selenium import webdriver
from selenium.webdriver import PhantomJS
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from sys import argv
import click
@click.command()
@click.option('--days', type=int, prompt='Number of days to set active lineup', help='Number of days to set active lineup')
@click.option('--username', prompt='Your Yahoo username:', help='Your Yahoo account username')
@click.option('--password', prompt='Your Yahoo passwordname:', help='Your Yahoo account password')
def start_active_players(days, username, password):
"""Simple python program that sets your active players for the next number DAYS."""
print("Logging in as: " + username)
dcap = DesiredCapabilities.PHANTOMJS.copy()
dcap['javascriptEnabled'] = True
dcap['browserConnectionEnabled'] = True
dcap['acceptSslCerts'] = True
dcap['localToRemoteUrlAccessEnabled'] = True
dcap['webSecurityEnabled'] = True
dcap['version'] = ''
driver = webdriver.PhantomJS(executable_path='/Users/devin.mancuso/node_modules/phantomjs/bin/phantomjs', desired_capabilities=dcap)
driver.get('https://login.yahoo.com/config/login?.src=spt&.intl=us&.done=http%3A%2F%2Fbasketball.fantasysports.yahoo.com%2Fnba')
with open('jquery-2.1.3.min.js', 'r') as jquery_js: jquery = jquery_js.read() #read the jquery from a file
driver.execute_script(jquery) #active the jquery lib
driver.find_element_by_id('login-username').send_keys(username)
driver.find_element_by_id('login-passwd').send_keys(password)
driver.implicitly_wait(8) # 8 seconds
driver.find_element_by_name('signin').click()
#form1 = driver.find_element_by_id('mbr-login-form')
#form1.submit()
driver.implicitly_wait(8) # 8 seconds
driver.save_screenshot('screenshot.png')
driver.find_element_by_xpath("//a[text() = 'My Team ']").click()
driver.implicitly_wait(8) # 8 seconds
for x in range(0, days):
driver.find_element_by_xpath("//a[text() = 'Start Active Players']").click()
driver.implicitly_wait(2) # 2 seconds
date_text = driver.find_element_by_xpath("//span[@class='flyout-title']").text
print("Starting active players for: " + date_text)
driver.find_element_by_xpath("//a[contains(@class, 'Js-next')]").click()
driver.implicitly_wait(2) # 2 seconds
driver.quit()
if __name__ == '__main__':
start_active_players()
脚本在第 47 行失败,
driver.find_element_by_xpath("//a[text() = 'My Team']").click()
当它试图找到带有文本“我的团队”的链接时。截图转储显示它永远不会通过登录表单。表格上方的屏幕错误消息指出
请重新加载页面重试或使用其他浏览器
我在 this post 中看到,因此包含了 execute_script 命令以在本地加载 Jquery,但这并没有解决问题。我不确定这是否是阻止 PhantomJS 的 Yahoo 安全问题。但为什么它只会在无头浏览器上失败,而不会在 FF 或 Chrome 上失败?
我也发现了this question并尝试提交表单本身而不是单击按钮,但这没有任何区别。我已经注释掉了上面示例中的代码。
PhantomJS 版本:2.0.0
解决方案是使用 python 绑定设置 PhantomJS userAgent。通过 Andrew Magee 在评论中的建议和 ghostdriver github 上的对话发现。
DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.userAgent'] = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0'
driver = webdriver.PhantomJS(executable_path='/Users/devin.mancuso/node_modules/phantomjs/bin/phantomjs')
这是一个美妙而美丽的网站。在线购买此新设计可享受免费国际送货服务。请访问我们的页面了解更多信息。