Get the width of the div on the server side with Selenium + PhantomJS + Python

It's a story now.

I want the width (px) of the div. You can easily get it on the browser side using JS, but this time I want it on the server side. Since it is the width of the div, it may be decided after rendering. So render on the server side. I used to do something with Selenium by putting Firefox on the server, but when installing Firefox, I have a memory that I want to spend time around the display, so this time I tried using PhantomJS as a browser.

http://phantomjs.org/

PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

That's right. amazing.

Add PhantomJS

Build PhantomJS just like this http://phantomjs.org/build.html Maybe because the server this time was CentOS5, it didn't work, and I changed it to git checkout 1.9 and it went smoothly. I'm in a hurry, so is this okay for the time being ...

Add Selenium

Next, I want to hit this from Selenium

pip install selenium

Play with PhantomJS from selenium

from selenium import webdriver
url = 'http://hoge'
driver = webdriver.PhantomJS()
driver.get(url)
e = driver.find_element_by_xpath("//*[@class='hoge']")
print(e.size)
>>> {'width': 602, 'height': 2098}

It seems to be good because it was the display result on my Chrome. The end

Recommended Posts

Get the width of the div on the server side with Selenium + PhantomJS + Python
Drawing tips with matplotlib on the server side
Get the operation status of JR West with Python
Get information equivalent to the Network tab of Chrome developer tools with Python + Selenium
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
Memo to get the value on the html-javascript side with jupyter
Get a list of purchased DMM eBooks with Python + Selenium
Access the host SQL Server with python27 / pyodbc on the container
Get the host name of the host PC with Docker on Linux
Get the source of the page to load infinitely with python.
Get the weather with Python requests
Get the weather with Python requests 2
How is the progress? Let's get on with the boom ?? in Python
Get started with the Python framework Django on Mac OS X
Get and set the value of the dropdown menu using Python and Selenium
Get the number of readers of a treatise on Mendeley in Python
PhytoMine-I tried to get the genetic information of plants with Python
Get html from element with Python selenium
Check the existence of the file with python
[Python] Get the variable name with str
Download files on the web with Python
Reboot the router using Python, Selenium, PhantomJS
[Python] Automatically operate the browser with Selenium
[Python] Get the character code of the file
Use selenium phantomjs webdriver with python unittest
Get a capture of the entire web page in Selenium Python VBA
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Edit the file of the SSH connection destination server on the server with VS Code
I tried to get the authentication code of Qiita API with Python.
Install the python module with pip on a server without root privileges
Get the number of articles accessed and likes with Qiita API + Python
I tried to get the movie information of TMDb API with Python
Get and estimate the shape of the head using Dlib and OpenCV with python
Get the return value of an external shell script (ls) with python3
Get the contents of git diff from python
[Python] Get the files in a folder with Python
Prepare the execution environment of Python3 with Docker
Automatic operation of Chrome with Python + Selenium + pandas
2016 The University of Tokyo Mathematics Solved with Python
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Get the size of the image file on the web (Python3, no additional library required)
[Python] Get / edit the scale label of the figure
[Note] Export the html of the site with python.
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
Calculate the total number of combinations with python
Get Unix time of the time specified by JST regardless of the time zone of the server in Python
Check the date of the flag duty with Python
[Python] Get rid of dating with regular expressions
[Python] Get the last updated date of the website
Fill the browser with the width of Jupyter Notebook
Get information on the 100 most influential tech Twitter users in the world with python.
Try out the touch of data-driven testing with Selenium Python Bindings and py.test
Introduction to Python with Atom (on the way)
Get CPU information of Raspberry Pi with Python
Get started with Python on macOS Big Sur
Note: How to get the last day of the month with python (added the first day of the month)
At the time of python update on ubuntu
Until you use PhantomJS with Python on Heroku
Convert the character code of the file with Python3
Freeze with send_keys of file selection when running Selenium WebDriver in Python [PhantomJS]