在Selenium中使用PhantomJS进行无头浏览器 - Python

时间:2016-01-30 00:24:00

标签: python selenium phantomjs

我目前正在使用$scope.$on('$destroy', function(){ angular.element( $document[0].getElementsByClassName('sanitized-style')).remove(); }); 来运行一些测试。它应该登录网页,输入Python-Seleniumusername,并做一些其他事情。当我使用password浏览器执行它时,它运行正常,但是当我使用Firefox时,我收到以下错误:

PhantonJS

稍微调试一下这个问题,我发现页面内容是:

2016-01-29 16:18:29 - ERROR - An exception occurred Message: {"errorMessage":"Unable to find element with id 'user_email'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"91","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:57257","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"id\", \"sessionId\": \"fcd54080-c6e6-11e5-94bd-27954be890c7\", \"value\": \"user_email\"}","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/fcd54080-c6e6-11e5-94bd-27954be890c7/element"}}
Screenshot: available via screen
Traceback (most recent call last):
  File "webcrawler.py", line 105, in login
    email = self.singleton.driver.find_element_by_id("user_email")
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 234, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: {"errorMessage":"Unable to find element with id 'user_email'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"91","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:57257","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"id\", \"sessionId\": \"fcd54080-c6e6-11e5-94bd-27954be890c7\", \"value\": \"user_email\"}","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/fcd54080-c6e6-11e5-94bd-27954be890c7/element"}}
Screenshot: available via screen

这是我开始DEBUG - <html><head></head><body></body></html>

Selenium driver

有人能告诉我如何解决这个问题吗?它使用Firefox完美运行,但我将在def __init__(self, browser="phantomjs"): if browser == "phantomjs": self.driver = webdriver.PhantomJS() self.driver.set_window_size(1120, 550) elif browser == "firefox": self.driver = webdriver.Firefox() elif browser == "chrome": self.driver = webdriver.Chrome() elif browser == "remote": self.driver = webdriver.Remote() elif browser == "ie": self.driver = webdriver.Ie() else: raise ValueError("Invalid browser value: %s" % browser) 的节点中部署它,所以我需要使用一些无头浏览器。

2 个答案:

答案 0 :(得分:2)

@alecxe给出的选项也是有效的。在我的特定情况下,网页使用SSL,因此解决方案是将PhantomJS创建为:

self.driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])

还要确保设置窗口大小,以便创建虚拟浏览器:

self.driver.set_window_size(1120, 550)

答案 1 :(得分:1)

首先,您仍然可以在AWS上使用Firefox,只需在虚拟显示下运行它:

至于你的PhantomJS特定问题,你需要一个wait - 等待元素的存在:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebdriverWait(driver, 10)

email_element = wait.until(EC.presence_of_element_located((By.ID, "user_email")))
email_element.send_keys(email)