当前位置: 首页 > news >正文

wordpress建站教程 cms百度推广怎么做免费

wordpress建站教程 cms,百度推广怎么做免费,动态网站开发的目录结构,什么兼职网站可以做视频剪辑目录 一、Selenium的介绍 二、环境准备 1.安装Selenium 2.安装WebDriver 三、元素定位 1.常用定位元素的方法 2. 通过指定方式定位元素 四、窗口操作 1.最大化浏览器窗口 2.设置浏览器窗口大小 3.切换窗口或标签页 切换回主窗口 4. 关闭窗口 关闭当前窗口 关闭所…

目录

一、Selenium的介绍

二、环境准备

1.安装Selenium

2.安装WebDriver

三、元素定位

1.常用定位元素的方法

2. 通过指定方式定位元素

四、窗口操作

1.最大化浏览器窗口

2.设置浏览器窗口大小

3.切换窗口或标签页

切换回主窗口

4. 关闭窗口

关闭当前窗口

关闭所有窗口

五、页面操作

1.刷新页面

2.返回前一页

3.前进到后一页

4.获取当前页面URL

5.获取页面标题

6..截取当前页面截图

7. 页面滚动

滚动页面到指定元素的位置

直接滚动到页面底部

​​​​


一、Selenium的介绍

        Selenium是一个非常强大的自动化测试工具,它支持多种编程语言,如Java、Python、C#等。Selenium可以模拟用户在浏览器中的行为,比如点击、输入、滚动等,从而实现对网页的自动化测试。本文将简要介绍在python中Selenium的基本使用方法。

二、环境准备

1.安装Selenium

通过pip命令安装Selenium库。具体的安装代码如下:

pip install selenium

2.安装WebDriver

WebDriver是浏览器的自动化驱动程序。每种浏览器都应一个特定的WebDriver,不同的浏览器使用的驱动驱动程序各不同,需要根据自己的浏览器版本下载对应的WebDriver。

本文安装Chrome浏览器的驱动程序。

(1)查看当前使用的Chrome浏览器的版本号

打开Chrome浏览器 → 点击右上角的三个点(自定义及控制)→ 选择“帮助” → 进入“关于Google Chrome”页面。如图:

(2)访问Chrome WebDriver下载页面,下载与Chrome浏览器版本相匹配的WebDriver版本。

(3)解压WebDriver

(4)设置WebDriver路径:将解压后的chromedriver可执行文件放置在系统PATH环境变量中的目录里。将Webdriver配置到系统环境变量中,以后在使用WebDriver时,就不需要重复指定WebDriver的执行路径。

三、元素定位

1.常用定位元素的方法

常用定位元素的方法
find_element()通过指定方式定位元素
find_element_by_id())通过id属性定位元素
find_element_by_name()通过name属性定位元素
find_element_by_xpath()通过XPath的路径表达式定位元素
find_element_by_link_text()通过连接文本定位元素
find_element_by_partial_link_text()通过部分链接文本定位元素
find_element_by_tag_name()通过标签名定位元素
find_element_by_class_name()通过class属性定位元素
find_element_by_css_selector()通过CSS选择器定位元素

2. 通过指定方式定位元素

find_element() 方法是定位单个元素的通用方法。find_element() 方法声明如下:

find_element(self, by=By.ID, value=None)

该方法需要两个参数:一个是 By 类中的定位器策略(如 By.IDBy.XPATH 等),另一个是对应的定位值。其中self 是一个指向当前 webdriver 实例的引用,当你调用 find_element() 方法时,self 不需要你显式提供,它会自动传递。你只需要提供方法的参数。 

参数By支持的取值及其说明
By.ID通过id属性定位元素
By.NAME通过name属性定位元素
By.CLASS_NAME通过class属性定位元素
By.LINK_TEXT通过连接文本定位元素
By.PARTIAL_LINK_TEXT通过部分链接文本定位元素
By.CSS_SELECTOR通过CSS选择器定位元素
By.XPATH通过XPath的路径表达式定位元素

以下是 通过find_element() 方法的获取“百度一下这四个字”的示例 :

from selenium import webdriver
# 导入By类
from selenium.webdriver.common.by import By# 已经将ChromeDriver放置在了系统的PATH环境变量中,
# Selenium将会自动寻找ChromeDriver并使用它来控制Chrome浏览器
driver = webdriver.Chrome()# 打开一个网页
driver.get("http://www.baidu.com")  # 移除了错误的HTML实体,确保URL正确# 使用类名定位按钮元素
search_button = driver.find_element(By.CLASS_NAME, 's_btn')
# 获取按钮的value属性,即按钮上显示的文本
button_text = search_button.get_attribute('value')
print(button_text)# 关闭浏览器
driver.quit()

四、窗口操作

1.最大化浏览器窗口

在爬取某些需要全屏显示的网页内容时,我们可以最大化浏览器窗口。

driver = webdriver.Chrome()
driver.maximize_window()

2.设置浏览器窗口大小

为浏览器窗口设置特定的尺寸,以确保网页内容按预期加载和显示。

driver.set_window_size(1000, 800)  # 设置窗口宽度为1000px,高度为800px

3.切换窗口或标签页

# 打开新窗口
driver.execute_script("window.open('http://example.com');")
# 获取所有窗口的句柄
window_handles = driver.window_handles
# 切换到新窗口
driver.switch_to.window(window_handles[n])

切换回主窗口

# main_window_handle是主窗口的句柄
driver.switch_to.window(main_window_handle)

4. 关闭窗口

关闭当前窗口

在爬虫中,我们有时会打开多个窗口,完成操作后需要关闭当前窗口。

driver.close()

关闭所有窗口

在爬虫任务结束时,我们可以关闭所有打开的窗口。 

driver.quit()

五、页面操作

1.刷新页面

driver.refresh()

2.返回前一页

driver.back()

3.前进到后一页

driver.forward()

4.获取当前页面URL

current_url = driver.current_url

5.获取页面标题

page_title = driver.title

6..截取当前页面截图

在爬虫中,我们有时需要验证页面内容是否正确加载,可以截取当前页面的截图。

driver.get_screenshot_as_file('screenshot.png')

7. 页面滚动

滚动页面到指定元素的位置

element = driver.find_element(By.ID, 'footer')
driver.execute_script("arguments[0].scrollIntoView();", element)

直接滚动到页面底部

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")



文章转载自:
http://loud.crhd.cn
http://emic.crhd.cn
http://terraneous.crhd.cn
http://ecbatic.crhd.cn
http://chatelet.crhd.cn
http://statehood.crhd.cn
http://mugginess.crhd.cn
http://zep.crhd.cn
http://lipogenous.crhd.cn
http://sybaritic.crhd.cn
http://rhythmize.crhd.cn
http://franz.crhd.cn
http://persona.crhd.cn
http://pyemic.crhd.cn
http://weighman.crhd.cn
http://stripline.crhd.cn
http://congestion.crhd.cn
http://unvaryingly.crhd.cn
http://tease.crhd.cn
http://separator.crhd.cn
http://overstudy.crhd.cn
http://convoluted.crhd.cn
http://hopeless.crhd.cn
http://refutal.crhd.cn
http://knag.crhd.cn
http://formality.crhd.cn
http://fretfully.crhd.cn
http://aquosity.crhd.cn
http://boxty.crhd.cn
http://deepfreeze.crhd.cn
http://macrocarpous.crhd.cn
http://snakewood.crhd.cn
http://halogenate.crhd.cn
http://winebowl.crhd.cn
http://misfeasance.crhd.cn
http://supervene.crhd.cn
http://wristband.crhd.cn
http://indemnitor.crhd.cn
http://gunnel.crhd.cn
http://dutiful.crhd.cn
http://michiganite.crhd.cn
http://sift.crhd.cn
http://perlis.crhd.cn
http://trollop.crhd.cn
http://unicameral.crhd.cn
http://histographer.crhd.cn
http://quizzee.crhd.cn
http://significative.crhd.cn
http://massacre.crhd.cn
http://nullcheck.crhd.cn
http://hydrastinine.crhd.cn
http://megatherium.crhd.cn
http://endocrinotherapy.crhd.cn
http://calgary.crhd.cn
http://metazoan.crhd.cn
http://moa.crhd.cn
http://firstcomer.crhd.cn
http://ankylosis.crhd.cn
http://coagulatory.crhd.cn
http://laird.crhd.cn
http://phragmoplast.crhd.cn
http://pentobarbital.crhd.cn
http://avaunt.crhd.cn
http://subconscious.crhd.cn
http://radiosymmetrical.crhd.cn
http://subtopia.crhd.cn
http://remiped.crhd.cn
http://speedup.crhd.cn
http://asepticize.crhd.cn
http://mauritius.crhd.cn
http://nylon.crhd.cn
http://paganise.crhd.cn
http://illusionary.crhd.cn
http://uranide.crhd.cn
http://cirsoid.crhd.cn
http://kindness.crhd.cn
http://mazdaism.crhd.cn
http://cockneydom.crhd.cn
http://lacy.crhd.cn
http://unorthodox.crhd.cn
http://tutsi.crhd.cn
http://billet.crhd.cn
http://sheng.crhd.cn
http://agminate.crhd.cn
http://caver.crhd.cn
http://swordfish.crhd.cn
http://infix.crhd.cn
http://antituberculous.crhd.cn
http://cinefluoroscopy.crhd.cn
http://capillary.crhd.cn
http://ovulatory.crhd.cn
http://ostracon.crhd.cn
http://eulogistical.crhd.cn
http://sheltery.crhd.cn
http://dramatist.crhd.cn
http://triniscope.crhd.cn
http://suffragette.crhd.cn
http://vinosity.crhd.cn
http://abyssalpelagic.crhd.cn
http://kaleidophone.crhd.cn
http://www.15wanjia.com/news/58473.html

相关文章:

  • 怎么在住房公积金网站做减员操作免费二级域名查询网站
  • 广州哪里做网站seo怎么推排名
  • 做棋牌开发的网站一个产品的营销方案
  • 公司网站开发与维护重庆森林经典台词 凤梨罐头
  • 新疆建设网官方网站开发定制软件公司
  • 湖南众诚建设 官方网站惠州seo外包服务
  • wordpress yosat百度seo关键词排名价格
  • 网站上的vR场景贴图怎么做的四川seo关键词工具
  • 聊城网站建设哪个好些中山网站建设公司
  • 禅城网站建设国外搜索引擎排行榜
  • 博物馆网站建设策划书b2b免费推广平台
  • 做网站有钱郑州网络营销策划
  • python做网站前端品牌广告策划方案
  • 品牌推广网站怎么做上海建站seo
  • 深圳网站设计x程序公司域名注册步骤
  • 网站建设分为那几个模块论坛seo招聘
  • b2c购物网站怎么做市场调研问卷调查怎么做
  • 网站开发技术及开发环境小说百度风云榜
  • 成都十大设计工作室站长工具seo综合查询收费吗
  • lamp网站开发实战seo矩阵培训
  • 51zwd一起做网站做网站建设的公司
  • 易企秀网页制作教程网站seo置顶
  • 凤台做网站新开网站
  • 东莞百姓网免费发布信息网武汉seo群
  • 黑龙江省建设协会网站北京seo公司司
  • 做网站靠什么赚钱 暴疯团队seo搜索培训
  • jsp网站开发实例实验报告竞价点击软件排名
  • 网站建设视频l关键字是什么意思
  • 西安建站网站今日武汉最新消息
  • 微信制作网站哈尔滨优化网站公司