安装selenium:
1.安装pip(VSCode Terminal 或 cmd命令行管理员方式):
pip install pip
也可直接搜索pip,到官网下载安装
2.安装selenium(VSCode Terminal 或 cmd命令行管理员方式):
pip install -U selenium
3.安装谷歌驱动 ChromeDriver:
其中,驱动版本要与谷歌版本相对应
不知道的话可以查看链接https://npm.taobao.org/mirrors/chromedriver/
将该驱动放置到python安装目录的根目录,(还有种说法是放到Scripts,感觉两种都没毛病的样子)
4.在VSCode中测试:
# -*- coding:utf-8 -*-
from selenium import webdriver
# create a new Firefox session
driver = webdriver.Chrome()
driver.implicitly_wait(10)#设置超时时间
driver.maximize_window()#窗口最大化显示
# navigate to the application home page
driver.get("http://www.baidu.com/")
# get the search textbox
search_field = driver.find_element_by_id("kw")#找到输入框
search_field.clear()#清空当前输入内容
# enter search keyword and submit
search_field.send_keys("铜钱贯")#重新这是搜索关键字
search_field.submit()#提交进行搜索
# get all the anchor elements which have product names displayed
# currently on result page using find_ elements_ by_ xpath method
products = driver.find_elements_by_xpath("//div[contains(@class, 'c-abstract')]")
# get the number of anchor elements found
print ("Found " + str(len(products)) + "products:")
# iterate through each anchor element and print the text that is
# name of the product
for product in products:
print (product.text)
# close the browser window
# driver.quit()
参考:1.https://blog.csdn.net/baby_hua/article/details/80571109