Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium with PhantomJS:Yahoo登录表单未提交(Python绑定)_Python_Selenium_Webdriver_Phantomjs_Ghostdriver - Fatal编程技术网

Selenium with PhantomJS:Yahoo登录表单未提交(Python绑定)

Selenium with PhantomJS:Yahoo登录表单未提交(Python绑定),python,selenium,webdriver,phantomjs,ghostdriver,Python,Selenium,Webdriver,Phantomjs,Ghostdriver,我正在编写一个python 2.7脚本,使用OS X上的selenium webdriver登录Yahoo fantasy sports并自动执行一些操作 该脚本可以与webDriver Firefox和Chromedriver配合使用。我最近开始使用PhantomJS(GhostDriver),我发现我无法让PhantomJS Selenium驱动程序(GhostDriver)登录 #/usr/bin/python 导入时间 从selenium导入webdriver 从selenium.web

我正在编写一个python 2.7脚本,使用OS X上的selenium webdriver登录Yahoo fantasy sports并自动执行一些操作

该脚本可以与webDriver Firefox和Chromedriver配合使用。我最近开始使用PhantomJS(GhostDriver),我发现我无法让PhantomJS Selenium驱动程序(GhostDriver)登录

#/usr/bin/python
导入时间
从selenium导入webdriver
从selenium.webdriver导入PhantomJS
从selenium.webdriver.common.keys导入密钥
从selenium.webdriver.chrome.options导入选项
从selenium.webdriver.common.action\u链导入ActionChains
从selenium.webdriver.common.desired_功能导入DesiredCapabilities
从系统导入argv
导入点击
@click.command()命令
@单击.option('--days',type=int,prompt='设置活动列表的天数',help='设置活动列表的天数〕)
@单击.option('--username',prompt='Your Yahoo username:',help='Your Yahoo account username')
@单击.option('--password',prompt='Your Yahoo passwordname:',help='Your Yahoo account password')
def start_active_播放器(天数、用户名、密码):
“”“一个简单的python程序,用于设置接下来几天的活动玩家。”“”
打印(“以“+用户名”身份登录)
dcap=DesiredCapabilities.PHANTOMJS.copy()
dcap['javascriptEnabled']=True
dcap['browserConnectionEnabled']=True
dcap['acceptSslCerts']=True
dcap['localToRemoteUrlAccessEnabled']=True
dcap['webSecurityEnabled']=True
dcap['version']='
driver=webdriver.PhantomJS(可执行文件路径='/Users/devin.mancuso/node\u modules/PhantomJS/bin/PhantomJS',所需的功能=dcap)
司机,上车https://login.yahoo.com/config/login?.src=spt&.intl=us&.done=http%3A%2F%2Fbasketball.fantasysports.yahoo.com%2Fnba')
使用open('jquery-2.1.3.min.js','r')作为jquery\u js:jquery=jquery\u js.read()#从文件中读取jquery
执行_脚本(jquery)#激活jquery库
驱动程序。通过\ id(“登录用户名”)查找\元素\发送\密钥(用户名)
驱动程序。通过\ id('login-passwd')查找\元素\发送\密钥(密码)
驱动程序。隐式等待(8)#8秒
驱动程序。按名称(“登录”)查找元素。单击()
#form1=驱动程序。通过\u id('mbr-login-form')查找\u元素
#表格1.提交()
驱动程序。隐式等待(8)#8秒
driver.save_屏幕截图('screenshot.png')
驱动程序。通过xpath(//a[text()='My Team'])查找元素。单击()
驱动程序。隐式等待(8)#8秒
对于范围内的x(0,天):
驱动程序。通过xpath(//a[text()='Start Active Players'])查找元素。单击()
驱动程序。隐式等待(2)#2秒
日期\文本=驱动程序。通过\ xpath(//span[@class='flyout-title'])查找\元素\文本
打印(“为:+日期\文本启动活动玩家”)
驱动程序。通过xpath(//a[contains(@class,'Js-next')])查找元素。单击()
驱动程序。隐式等待(2)#2秒
driver.quit()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
启动活动玩家()
脚本在第47行失败

驱动程序。通过xpath(//a[text()='My Team'])查找元素。单击()

当它试图找到带有文本My Team的链接时。截图转储显示它从未通过登录表单。窗体状态上方的屏幕错误消息

请重新加载页面,然后重试或使用其他浏览器

我在中看到并因此包含了要本地加载到Jquery中的execute_脚本命令,但这并没有解决它。我不确定阻止PhantomJS的是不是雅虎的安全问题。但为什么它只能在无头浏览器上失败,而不能在FF或Chrome上失败呢

我还发现并尝试提交表单本身,而不是单击按钮,但这没有什么区别。我已经注释掉了上面示例中的代码


PhantomJS版本:2.0.0

解决方案是使用python绑定设置PhantomJS用户代理。通过Andrew Magee在评论中的建议,以及通过网站上的对话发现


也许可以尝试使用Firefox或Chrome驱动程序,看看是否有效,然后你就会知道这是否是浏览器特有的问题?使用Selenium中的webDriver(FF)和ChomeDriver(Chrome)驱动程序,代码执行时不会出现问题。雅虎似乎对PhantomJS驱动程序如何提交表单存在问题。也许可以尝试摆弄
userAgent
?我不是100%确定,但我认为只有当您单独使用PhantomJS时,这才有效,而不是针对Selenium的PhantomJS ghostdriver驱动程序。这是和,似乎userAgent不在其中。我也不确定,但从源代码看,您可能能够将
'phantomjs.page.settings.userAgent':'…'
放在您的
所需功能中?(查看第114-124行。)
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')