JavaEar 专注于收集分享传播有价值的技术资料

enable cookies in Phantomjs + selenium

I want to login in amazons3 (with the url:'https://console.aws.amazon.com/iam/home?#security_credential') on my armv7 board.I used phantom2.0.1 from here and selenium2.45.0.

I can open the website successfully, but when I doing 'submit' after filling the username and password, the website jump to an error page which shows ‘Please Enable Cookies to Continue’. So I wonder how to enable cookies for phantomjs in selenium.In my ubuntu12.04 with suitable version of phantomjs, I can succeed

Part of my code is below:

def __init__(self,username,password,login_url,width=1151,height=629):
    self.username = username
    self.password = password
    self.login_url = login_url
    dcap = dict(DesiredCapabilities.PHANTOMJS)  
    dcap["phantomjs.page.settings.userAgent"] = ( "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) Phantomjs/2.0.1 Safari/534.34" )  
    self.driver = webdriver.PhantomJS(executable_path='/bin/phantomjs',desired_capabilities=dcap,service_args=['--ssl-protocol=any','--ignore-ssl-errors=true'])
    self.driver.set_window_size(width,height)

def _login_system(self):
    try:
        self.driver.get(self.login_url)
        print self.driver.page_source#I can success here
        WebDriverWait(self.driver, 30).until(lambda driver : self.driver.find_element_by_id("ap_email")).send_keys(self.username)
        WebDriverWait(self.driver, 30).until(lambda driver : self.driver.find_element_by_id("ap_password")).send_keys(self.password)
        WebDriverWait(self.driver, 30).until(lambda driver : self.driver.find_element_by_id("signInSubmit-input")).submit()#failed here and showing Please Enable Cookies to Continue

1个回答

    最佳答案
  1. I've found the answer, It seems that amazon does not accept "Phantomjs/(..*)" in the useragent

    fail:Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) Phantomjs/2.0.1 Safari/534.34

    OK:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:36.0) Gecko/20100101 Firefox/36.0 WebKit

    In addition,I add "WebKit" at the end of the useragent,because of the problem "undefined is not an object"(https://github.com/detro/ghostdriver/issues/325)

    What's more,I've met a problem of "raise BadStatusLine(line)"(like here:enter link description here).And it may be caused by the dismatch of the versions of selenium and phantomjs,so I use phantomjs1.9.8 instead(piksel/phantomjs-raspberrypi in github)

    For the compile problem,if you are to compile on an arm board,you'd better use swap to extend the RAM.