Selenium入门教程 hello world (Python + Chrome + VSCode)

安装selenium:

1.安装pip(VSCode Terminal 或 cmd命令行管理员方式):

pip install pip

也可直接搜索pip,到官网下载安装

2.安装seleniumVSCode 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

           2.https://www.cnblogs.com/suijing-luck/p/11468978.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值