我对PhantomJS
有一个奇怪的问题,或者我可能是新手。我正在尝试使用PhantomJS通过Selenium登录NewEgg.com。我正在使用Python。问题是,当我使用Firefox作为驱动程序时,它运行良好,但是一旦我将PhantomJS
设置为驱动程序,它就不会转到下一页,因此会给出消息:
Exception Message: u'{"errorMessage":"Unable to find element with id \'UserName\'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"89","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:55372","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"using\\": \\"id\\", \\"sessionId\\": \\"aaff4c40-6aaa-11e4-9cb1-7b8841e74090\\", \\"value\\": \\"UserName\\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/aaff4c40-6aaa-11e4-9cb1-7b8841e74090/element"}}' ; Screenshot: available via screen
我在截图后发现幻像无法浏览页面并完成脚本的原因。我该如何解决这个问题?我尝试过的代码片段如下:
导入请求 来自bs4进口BeautifulSoup 从时间导入睡眠 来自selenium import webdriver 导入日期时间
my_username = "user@mail.com"
my_password = "password"
driver = webdriver.PhantomJS('/Setups/phantomjs-1.9.7-macosx/bin/phantomjs')
firefox_profile = webdriver.FirefoxProfile()
#firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
#driver = webdriver.Firefox(firefox_profile)
driver.set_window_size(1120, 550)
driver.get('http://newegg.com')
driver.find_element_by_link_text('Log in or Register').click()
driver.save_screenshot('screen.png')
我甚至放了sleep
,但它没有任何区别。
答案 0 :(得分:0)
当第二页的内容类型不正确时,我在使用PhantomJS时遇到过这种情况。普通的浏览器只是动态地解释内容,但是Phantom只是默默地死去。