Selenium库,Python精品教程!

selenium库必须精练,Python爬虫无极限!

什么是Selenium

selenium库必须精练,Python爬虫无极限!

selenium基本使用

用python写爬虫的时候,主要用的是selenium的Webdriver,我们可以通过下面的方式先看看Selenium.Webdriver支持哪些浏览器

selenium库必须精练,Python爬虫无极限!

执行结果如下,从结果中我们也可以看出基本支持了常见的所有浏览器:

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

声明浏览器对象

知道了selenium支持很多的浏览器,但是如果想要声明并调用浏览器则需要:

selenium库必须精练,Python爬虫无极限!

这里只写了两个例子,当然了其他的支持的浏览器都可以通过这种方式调用。

访问页面

selenium库必须精练,Python爬虫无极限!

python代码运行后,会自动打开Chrome浏览器,并登陆百度打印百度首页的源代码,然后关闭浏览器。

查找元素

单个元素查找

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

多个元素查找

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

元素交互操作

对于获取的元素调用交互方法

selenium库必须精练,Python爬虫无极限!

运行的结果可以看出程序会自动打开Chrome浏览器并打开淘宝输入ipad,然后删除,重新输入MakBook pro,并点击搜索。

交互动作

将动作附加到动作链中串行执行

selenium库必须精练,Python爬虫无极限!

执行JavaScript

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

获取元素属性

selenium库必须精练,Python爬虫无极限!

获取文本值

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

Frame

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

等待

selenium库必须精练,Python爬虫无极限!

隐式等待

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

显示等待

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

浏览器的前进和后退

back()

forward()

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

选项卡管理

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

异常处理

这里只进行简单的演示,查找一个不存在的元素

selenium库必须精练,Python爬虫无极限!

selenium库必须精练,Python爬虫无极限!

猜你喜欢

转载自www.cnblogs.com/tzjks/p/9061213.html
今日推荐