Python+Selenium automates Chrome to simulate mobile browser

When using Chrome to browse the web, we can use Chrome developer tools to simulate mobile browsers, and we can also simulate mobile browsers when using Selenium to operate Chrome. There are mainly two uses.

  1. Test whether the display of the H5 page is normal on devices with different resolutions
  2. Crawling data (general websites have weak anti-crawling teaching for mobile device browsing)

Use specified equipment

The operation method is very simple. In the ChromeOptions() browser option, add the experimental option, and specify the selected device through the devicename in the mobileEmulation option. The operation code is as follows.

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', {'deviceName': 'iPhone X'})  # 模拟iPhone X浏览
driver = webdriver.Chrome(options=options)

driver.get('http://m.baidu.com')

The operation effect is as follows:

There are many types of devices that have been set in Chrome Developer Tools, which can be viewed in Developer Tools->Settings->Devices, as shown in the figure below:

Use a custom device

In addition to specifying devices through deviceName, mobileEmulation can also specify device metrics through deviceMetrics. General device metrics include

  • width: device width
  • height: device height
  • piexelRatio: device pixel density
  • userAgent: device browser ID

An example of use is as follows:

from selenium import webdriver
options = webdriver.ChromeOptions()

options.add_experimental_option('mobileEmulation', 
    {'deviceMetrics':{'width': 320, 
                      'height': 640, 
                      'piexelRatio': 3.0, 
                      'userAgent': 'Mozilla/5.0 (Linux; Android 4.1.1; GT-N7100 Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/35.0.1916.138 Mobile Safari/537.36 T7/6.3'
                      }
    }
    )

driver = webdriver.Chrome(options=options)
driver.get('http://m.baidu.com')

Of course, if you only need to test different resolutions, you can only set the width and height values.

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/m0_59868866/article/details/130345576