Is there a way to use PhantomJS in Python?

后端 未结 8 1407
死守一世寂寞
死守一世寂寞 2020-11-22 08:05

I want to use PhantomJS in Python. I googled this problem but couldn\'t find proper solutions.

I find os.popen() may be a good choice. But I couldn\'t

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 08:13

    The answer by @Pykler is great but the Node requirement is outdated. The comments in that answer suggest the simpler answer, which I've put here to save others time:

    1. Install PhantomJS

      As @Vivin-Paliath points out, it's a standalone project, not part of Node.

      Mac:

      brew install phantomjs
      

      Ubuntu:

      sudo apt-get install phantomjs
      

      etc

    2. Set up a virtualenv (if you haven't already):

      virtualenv mypy  # doesn't have to be "mypy". Can be anything.
      . mypy/bin/activate
      

      If your machine has both Python 2 and 3 you may need run virtualenv-3.6 mypy or similar.

    3. Install selenium:

      pip install selenium
      
    4. Try a simple test, like this borrowed from the docs:

      from selenium import webdriver
      from selenium.webdriver.common.keys import Keys
      
      driver = webdriver.PhantomJS()
      driver.get("http://www.python.org")
      assert "Python" in driver.title
      elem = driver.find_element_by_name("q")
      elem.clear()
      elem.send_keys("pycon")
      elem.send_keys(Keys.RETURN)
      assert "No results found." not in driver.page_source
      driver.close()
      

提交回复
热议问题