phantomjs series those pits that Selenium+Phantomjs climbed

The exploration of technology is to constantly make assumptions and then continually overthrow it

Recently, when I was writing reptiles with my colleagues using phantomjs, I encountered a lot of interesting pits. After analyzing them, we came to some conclusions and solutions. Let’s share them.

The cause of the problem is because we want to use phantomjs to access a batch of websites to obtain the source code and url. Then when we look at the output, we find that the requested url does not correspond to the url obtained after the visit. For example, I use phantomjs to access baidu, and return The result shows that the current url is bing. This has led us to a series of conjectures. Because there are relatively few resources on the Internet, we can only guess and test it ourselves.
For the problem that the result value does not correspond, I temporarily define that the phantomjs state is contaminated or covered. To put it simply, we first go to the a website. After getting the results, we visit the b website and get the results of the b website. However, we found that the result of the b website is a website. Then we first think that when phantomjs reprocesses b website, its status is not updated, and the result of obtaining b website is still a website.
So what is the cause of the phantomjs state not updated?
My colleague’s blog details two reasons. For details, please see:, which will not be repeated here. .

As a supplementary explanation, I posted the test code here for reference.

phantomjs state pollution test

Test code

D.implicitly_wait(10) ##Set timeout
D.set_page_load_timeout(10) ##Set timeout time
def gethttp(url):
except Exception,e:
print is
print d.current_url

When we first run with phantomjs, then run the website, and notice that is not accessible.

Gethttp("") #Web site can open normally
Gethttp("") #DNS resolution failed, the website can not open

Results of the:


We can see that we have returned to when we obtained the information on the website.

When we visit a web page source with a page with the onbeforeunload element.

Gethttp("") #page memory onbeforeunload element
Gethttp("") #Web site can open normally

Results of the:


It can be seen that the above two cases will lead to phantomjs state pollution, and other conditions are still subject to later observation and testing.


Thorough Law

Each time d.get() requests, d.quit() closes the phantomjs process and waits until a new request is opened. (very resource intensive)

Every time you get to determine whether the url can be parsed by dns, whether the url can be opened. (also a bit resource consuming)

Elegant Method

After each get, save the value of current_url, compare it with this value after the next request, if it is the same, it means that the state has not been changed.
(Of course, except for some special cases, such as the same website for each get, or the same address in the batch get website.)

# # # Supertheism

After each time you get a target url, go to the next get (“about:blank”) and reset the state.


[[phantomjs series] phantomjs correctly opened] ( 80%E6%96%B9%E5%BC%8F/)
[[phantomjs series] phantomjs api introduction] (
[[Phantomjs series] those pits that selenium+phantomjs climbed] ( %82%A3%E4%BA%9B%E5%9D%91/)
[[phantomjs series] selenium+phantomjs performance optimization] ( 96/)

本文标题:phantomjs series those pits that Selenium+Phantomjs climbed


发布时间:2017年03月01日 - 16:03

最后更新:2019年08月16日 - 15:08

原始链接: pits that Phantomjs climbed/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat