我正在尝试编写一个自动登录到两个网站并转到某个页面的代码。我使用Splinter。
我只使用PhantomJS作为浏览器类型在“Mijn ING Zakelijk”网站上收到错误。
直到几天前,代码在20次中完成了20次。但是从今天起我就收到了错误。有时代码运行正常。其他时候它没有,并给我“点击成功,但加载失败..”错误。这是完整的追溯:
## Attempting to login to Mijn ING Zakelijk, please wait.
- Starting the browser..
- Visiting the url..
- Filling the username form with the defined username..
- Filling the password form with the defined password..
- Clicking the submit button..
Traceback (most recent call last):
File "/Users/###/Dropbox/Python/Test environment 2.7.3/Splinter.py", line 98, in <module>
mijning()
File "/Users/###/Dropbox/Python/Test environment 2.7.3/Splinter.py", line 27, in mijning
attemptLogin(url2, username2, password2, defined_title2, website_name2, browser_type2)
File "/Users/###/Dropbox/Python/Test environment 2.7.3/Splinter.py", line 71, in attemptLogin
browser.find_by_css('.submit').first.click()
File "/Users/###/Library/Python/2.7/lib/python/site-packages/splinter/driver/webdriver/__init__.py", line 344, in click
self._element.click()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 54, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 228, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: u'Error Message => \'Click succeeded but Load Failed. Status: \'fail\'\'\n caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:56899","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"sessionId\\": \\"c2bbc8a0-e3d2-11e2-b7a8-f765797dc4e7\\", \\"id\\": \\":wdc:1372850513087\\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/c2bbc8a0-e3d2-11e2-b7a8-f765797dc4e7/element/%3Awdc%3A1372850513087/click"}' ; Screenshot: available via screen
Process finished with exit code 1
这是完整的代码:
## *** Payment Notification and Mail Tool (FPNMT) ##
from splinter import *
from Tkinter import *
def ###():
# Define values
browser_type1 = 'phantomjs' # 'phantomjs' or 'firefox'
url1 = 'http://###.nl/admin'
username1 = '###'
password1 = '###'
defined_title1 = 'Bestellingen'
website_name1 = '###.nl Admin'
attemptLogin(url1, username1, password1, defined_title1, website_name1, browser_type1)
def mijning():
# Define values
browser_type2 = 'phantomjs' # 'phantomjs' or 'firefox'
url2 = 'https://mijnzakelijk.ing.nl/internetbankieren/SesamLoginServlet'
username2 = '###'
password2 = '###'
defined_title2 = 'Saldo informatie'
website_name2 = 'Mijn ING Zakelijk'
attemptLogin(url2, username2, password2, defined_title2, website_name2, browser_type2)
# Functions #
def attemptLogin(url, username, password, defined_title, website_name, browser_type):
print '## Attempting to login to ' + website_name + ', please wait.'
# Start the browser
print '- Starting the browser..'
browser = Browser(browser_type)
# Visit in the url
print '- Visiting the url..'
browser.visit(url)
if website_name == '###.nl Admin':
# Find the username form and fill it with the defined username
print '- Filling the username form with the defined username..'
browser.fill('username', username)
# Find the password form and fill it with the defined password
print '- Filling the password form with the defined password..'
browser.fill('password', password)
# Find the submit button and click
print '- Clicking the submit button..'
browser.click_link_by_text('Inloggen')
# Find, click and display page with order history
print '- Visiting the defined web page..'
current_token = browser.url[57:97]
url_plus_token = 'http://www.###.nl/admin/index.php?route=sale/order' + current_token
browser.visit(url_plus_token)
else:
website_name == 'Mijn ING Zakelijk'
# Find the username form and fill it with the defined username
print '- Filling the username form with the defined username..'
browser.find_by_id('gebruikersnaam').first.find_by_tag('input').fill(username)
# Find the password form and fill it with the defined password
print '- Filling the password form with the defined password..'
browser.find_by_id('wachtwoord').first.find_by_tag('input').fill(password)
# Find the submit button and click
print '- Clicking the submit button..'
browser.find_by_css('.submit').first.click()
# Display page with transaction history
print '- Visiting the defined web page..'
browser.visit('https://mijnzakelijk.ing.nl/mpz/solstartpaginarekeninginfo.do')
# Get page title after successful login
current_title = browser.title
# Check the title of the page to confirm successful login
checkLogin(defined_title, current_title, website_name, browser)
def checkLogin(defined_title, current_title, website_name, browser):
if current_title == defined_title:
print '# Login to', website_name, 'successful.'
print '- Quitting the browser..'
browser.quit()
else:
print '# Login to', website_name, 'failed.'
print '- Quitting the browser..'
browser.quit()
i = 1
while i < 10:
print i
#***()
mijning()
i = i+1
关于导致此错误的原因的任何想法以及如何解决?
感谢。
答案 0 :(得分:5)
当前版本的ghostdriver源代码修复了这个问题(不再有“Click succeeded but Load Failed”消息“ - 请参阅here)。问题是,该版本尚未发布(如(2013年8月19日),所以你需要得到它然后自己构建它。这解决了我的问题(Windows 7,Python 2.7.5,Selenium 2.33)。你可以找到一步一步{{ 3}}
<强>更新强>:
PhantomJS 1.9.2刚出来并使用Ghostdriver 1.0.4修复了问题(检查here - 不再“点击成功但加载失败”消息)。所以只需升级到here即可。不需要自己构建任何东西了。
答案 1 :(得分:2)
可能是页面上已经存在一些活跃的javascript或后台AJAX,这让PhantomJS认为按钮点击不成功。您可以尝试插入等待或尝试在点击之前停止浏览器。
答案 2 :(得分:0)
这是fixed bug,但在某些实际页面中显示。
解决方法:
public static void ClickAndWaitStale( this IWebElement element , DateTime limit = default(DateTime) , TimeSpan sleepBetween = default(TimeSpan) )
{
if ( limit == default(DateTime) )
limit = DateTime.Now.AddSeconds( 300 );
if ( sleepBetween == default(TimeSpan ) )
sleepBetween = TimeSpan.FromSeconds( 1 );
try
{
element.Click();
}
catch ( System.InvalidOperationException ) // Click succeeded but Load Failed
{
}
while ( DateTime.Now < limit )
{
try
{
element.GetAttribute( "id" );
Thread.Sleep( sleepBetween );
}
catch ( StaleElementReferenceException )
{
return;
}
}
throw new TimeoutException( "Timeout waiting for stale object." );
}