Failed to load resource: the server responded with

2019-08-28 04:40发布

问题:

I've been in the process of making an instagram bot for a few days now and I'm having trouble logging into instagram with a headless selenium browser.

The script I made works perfectly fine when run on my laptop but when I try to run this on my digital ocean server, the browser will fill the login forms, then submit the form but nothing happens. I was able to print any console errors with this code:

for entry in self.browser.get_log('browser'):
        print(str(entry))

and this error comes up

{u'source': u'network', u'message': u'https://www.instagram.com/accounts/login/ajax/ - Failed to load resource: the server responded with a status of 400 ()', u'timestamp': 1546536937734, u'level': u'SEVERE'}

I am using the chromedriver for selenium and python2.7

I'm not completely sure why this is happening. Thanks for any help!

回答1:

This error message...

{u'source': u'network', u'message': u'https://www.instagram.com/accounts/login/ajax/ - Failed to load resource: the server responded with a status of 400 ()', u'timestamp': 1546536937734, u'level': u'SEVERE'}

...implies that the WebDriver was unable to initiate/spawn a new WebBrowsing Session as it failed to load the required resources through AJAX calls.

As per the HTML DOM of the Instagram - Log in page it is pretty clear the DOM Tree contains AJAX and JavaScript enabled elements.

So while you invoke get() method before interacting with the elements on the particular you need to induce WebDriverWait for the desired elements to be clickable which will ensure that:

  • The associated JavaScript and AJAX Calls have completed
  • The desired elements are enabled and visible to recognize click events propogated through Selenium.

You can find a relevant discussion in Google adsense responding the server responded with a status of 400 ()

Here you can find a relevant discussion on Filling in login forms in Instagram using selenium and webdriver (chrome) python OSX