How to switch to smartphone mode with Python + Selenium + Chrome

1. 1. Overview

I was doing an automatic test using Python + Selenium + Chrome, but when I accessed the smartphone screen, I was redirected to the PC screen and sometimes I could not test the smartphone screen.

Therefore, I investigated whether it can be automated well using Chrome's smartphone mode, so I will summarize the method.

2. Program to start in smartphone mode

Summarizing the results of the investigation, it seems that it can be started in smartphone mode by executing the following program. (It works even if you copy and paste it as it is)

smp_mood.py


from selenium import webdriver
import time

def smp_mood():
	url = "https://www.yahoo.co.jp/"

	mobile_emulation = { "deviceName": "Galaxy S5" }
	options = webdriver.ChromeOptions()
	options.add_experimental_option("mobileEmulation", mobile_emulation)
	driver = webdriver.Chrome(options=options)

	driver.get(url)
	time.sleep(2)

smp_mood()

The above is accessing the Yahoo site with "Galaxy S5".

3. How to switch terminals

Of the above program

"Galaxy S5"

It is possible to change to the terminal you want to use by rewriting.

For the device that can be switched, right-click on Chrome and press "Verify". After that, press the smartphone mark on the upper right of the screen to change to the smartphone mode, and then press "Terminal name> Edut ..." displayed on the upper left of the screen to display the list of terminals that can be changed.

image.png

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

image.png

Based on the above

"iPhone 6"

You can change the target terminal by rewriting as.

Recommended Posts

How to switch to smartphone mode with Python + Selenium + Chrome
How to automatically install Chrome Driver for Chrome version with Python + Selenium + Chrome
How to use python interactive mode with git bash
How to download files from Selenium in Python in Chrome
Python: How to use async with
How to get started with Python
How to use FTP with Python
How to scrape at speed per second with Python Selenium
How to do portmanteau test with python
How to display python Japanese with lolipop
How to switch python versions in cloud9
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to install python3 with docker centos
[Python] How to set the (client) window size inside the browser with Selenium
How to upload with Heroku, Flask, Python, Git (4)
How to read a CSV file with Python 2/3
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
Strategy on how to monetize with Python Java
Automatic operation of Chrome with Python + Selenium + pandas
Try running Google Chrome with Python and Selenium
[Python] How to draw multiple graphs with Matplotlib
[Python] How to read excel file with pandas
How to crop an image with Python + OpenCV
How to deal with SessionNotCreatedException when using Selenium
Log in to Yahoo Business with Selenium Python
How to specify attributes with Mock of python
How to measure execution time with Python Part 1
How to use tkinter with python in pyenv
[Python] How to handle Japanese characters with openCV
[Python] How to compare datetime with timezone added
How to measure execution time with Python Part 2
How to install Python
How to install python
How to debug selenium
ScreenShot with Selenium (Python)
Scraping with Selenium [Python]
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
How to convert / restore a string with [] in python
How to add help to HDA (with Python script bonus)
[Python] How to draw a line graph with Matplotlib
How to scrape image data from flickr with python
How to do hash calculation with salt in Python
[Introduction to Python] How to iterate with the range function?
Explain in detail how to make sounds with python
How to upload with Heroku, Flask, Python, Git (Part 3)
[Python] How to specify the download location with youtube-dl
How to measure mp3 file playback time with python
How to convert JSON file to CSV file with Python Pandas
How to upload with Heroku, Flask, Python, Git (Part 1)
[Python] How to deal with pandas read_html read error
How to upload with Heroku, Flask, Python, Git (Part 2)
How to get mouse wheel verdict with Python curses
[Python] How to rewrite the table style with python-pptx [python-pptx]
Automate Chrome with Python and Selenium on your Chromebook
[Python] How to create a 2D histogram with Matplotlib
How to operate Discord API with Python (bot registration)
[Python] How to draw a scatter plot with Matplotlib
How to operate Firefox with selenium on Windows Memo