【问题标题】:Using PhantomJS for headless browser in Selenium - Python在 Selenium 中将 PhantomJS 用于无头浏览器 - Python
【发布时间】:2016-01-30 00:23:59
【问题描述】:

我目前正在使用Python-Selenium 运行一些测试。它应该是登录网页,输入usernamepassword,然后做一些其他的事情。当我使用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

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)

谁能告诉我如何解决这个问题?它使用 Firefox 可以完美运行,但我将把它部署在 AWS 的一个节点中,所以我需要使用一些无头浏览器。

【问题讨论】:

    标签: python selenium phantomjs


    【解决方案1】:

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

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

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

    self.driver.set_window_size(1120, 550)
    

    【讨论】:

    • 是的,我记得使用 --ignore-ssl-errors 标志也可以解决不同的 PhantomJS 特定问题。谢谢。
    【解决方案2】:

    首先,你仍然可以在 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2013-01-01
      • 1970-01-01
      • 2017-01-02
      相关资源
      最近更新 更多