Python uses selenium's webdriver module to automatically open web pages and search

  1. Prerequisite
    Selenium is a third-party module and needs to be installed in advance.
    pip install selenium

  2. Download Insert picture description here
    and put the decompressed geckodriver.exe into the path. (The installation path of python in this article has already set the PATH variable, so put it directly in the installation folder of python)
    Insert picture description here

  3. Open the Baidu homepage in the browser, locate the search box with the mouse, find the label of the search box, and find the id value of the tag is "kw"

Insert picture description here

  1. python implementation code
from selenium import webdriver
print("开始")
driver = webdriver.Firefox()
print("开始1")
url = 'http://www.baidu.com'
driver.get(url)
print("开始2")
el = driver.find_element_by_id("kw")# 通过id 来定位
print("开始3")
el.send_keys('selenium')# 向定位的搜索框传入搜索内容“selenium"
  1. Achieve effect
    Insert picture description here

Guess you like

Origin blog.csdn.net/KathyLJQ/article/details/106723470