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

东营建设局网站百度下载官网

东营建设局网站,百度下载官网,西安异构国际设计,深圳新闻今日头条目录 1. 安装 2. 测试 3. 无头浏览器 4. 元素定位 5. 页面滑动 6. 按键、填写登录表单 7. 页面切换 Selenium是Web的自动化测试工具,为网站自动化测试而开发,Selenium可以直接运行在浏览器上,它支持所有主流的浏览器,可以接…

目录

1. 安装

2. 测试

3. 无头浏览器

4. 元素定位

5. 页面滑动

6. 按键、填写登录表单

7. 页面切换


Selenium是Web的自动化测试工具,为网站自动化测试而开发,Selenium可以直接运行在浏览器上,它支持所有主流的浏览器,可以接收指令,让浏览器自动加载界面,获取需要的数据,页面截屏。

1. 安装

浏览器:谷歌、火狐、Edge,这些浏览器的内核都是google

打开浏览器设置,查看浏览器版本

打开chromedriver下载网页,选择一个和浏览器内核版本最接近的一个版本:CNPM Binaries Mirrorhttps://registry.npmmirror.com/binary.html?path=chromedriver/点击进入,此处以windows环境为示例:

下载安装包,并解压这个文件,得到这个exe文件

如果你是使用PyCharm自带的Python解释器,那么你需要将这个文件放入你的PyCharm文件的bin目录下,例如:C:\...\PyCharm Community Edition 2022.1.3\bin

如果你是通过PyCharm使用Anaconda虚拟环境,那么你需要将这个文件放入你的Anaconda文件的Scripts目录下,例如:C:\...\Anaconda3\Scripts

添加环境变量,如果你已经是使用过PyCharm的用户,那么你的PyCharm大概率是已经添加进入环境变量了,此时你不用再添加环境变量

2. 测试

写一个访问浏览器页面的测试代码,首先下载selenium模块

from selenium import webdriver
import time# 这两个方法二选一,webdriver.Chrome()会真的打开一个浏览器
# driver = webdriver.PhantomJS()
driver = webdriver.Chrome()# 访问浏览器网址
driver.get('https://www.douban.com/')# 截图保存图片
driver.save_screenshot("首页.png")# 页面停留时间
time.sleep(5)# 退出当前页面
driver.close()# 退出浏览器
driver.quit()

运行成功会打开浏览器豆瓣首页网址并停留5秒,拿到首页截图。

3. 无头浏览器

无头浏览器不会打开浏览器页面,但会访问网页,适用于Linux环境

最新的selenium已经放弃了Phantomjs,直接将无头浏览器的逻辑进行了整合

from selenium import webdriver# 无头
from selenium.webdriver.chrome.options import Options# 配置参数
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")# 把参数配置给浏览器
driver = webdriver.Chrome(options=opt)driver.get("https://www.douban.com/")
driver.save_screenshot("./selenium_test/首页1.png")
driver.close()
driver.quit()

4. 元素定位

from selenium import webdriver
import time
from lxml import etree# 无头
from selenium.webdriver.chrome.options import Options# 元素定位
from selenium.webdriver.common.by import By# 配置参数
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")# 把参数配置给浏览器
driver = webdriver.Chrome(options=opt)driver.get("https://book.douban.com/")# 截屏当前页面
driver.save_screenshot("./selenium_test/tv.png")# 获取前端代码
test = driver.page_source
# print(test)# html = etree.HTML(test)
# xpath_result = html.xpath('//*[@id="content"]/div/div[1]/div[1]/div[2]/div[1]/div/ul[2]/li')
# print(xpath_result)
# print(len(xpath_result))
# for i in xpath_result:
#     print(i.xpath('.//div[@class="info"]//a/@title'))# 元素定位
xpath_result = driver.find_element(By.XPATH, '//*[@id="content"]/div/div[1]/div[1]/div[2]/div[1]/div/ul[2]/li[1]')
print(xpath_result)
print(xpath_result.text)time.sleep(3)
driver.close()
driver.quit()

5. 页面滑动

from selenium import webdriver
import timedriver = webdriver.Chrome()driver.get("https:/www.douban.com/")time.sleep(2)
js = 'window.scrollTo(0, 10000)'   # 向下滑
# js = 'window.scrollTo(10000, 0)' # 向左滑
# js = 'window.scrollTo(10000, 10000)' # 向左并向下滑
driver.execute_script(js)
time.sleep(2)       # 向上海
js = 'window.scrollTo(0, -10000)'
driver.execute_script(js)time.sleep(5)driver.close()
driver.quit()

6. 按键、填写登录表单

from selenium import webdriver
import time
from selenium.webdriver.common.by import Bydriver = webdriver.Chrome()try:driver.get('https://book.douban.com/')time.sleep(3)btn = driver.find_element(By.LINK_TEXT, '登录/注册')btn.click()# url = driver.current_url# driver.get(url)driver.find_element(By.XPATH, '//*[@id="account"]/div[2]/div[2]/div/div[1]/ul[1]/li[2]').click()time.sleep(2)# 填写登录表单driver.find_element(By.XPATH, '//*[@id="username"]').send_keys('12345678')driver.find_element(By.XPATH, '//*[@id="password"]').send_keys('202125DOUBAN')time.sleep(2)driver.find_element(By.XPATH, '//*[@id="account"]/div[2]/div[2]/div/div[2]/div[1]/div[4]/a').click()time.sleep(10)driver.close()driver.quit()except:print(Exception)

7. 页面切换

不同的网站有不同的应有场景,有些网站不会新生成页面,有些网站可以自动跳转

from selenium import webdriver
from selenium.webdriver.common.by import By
from lxml import etree
import timeurl = 'https://www.bilibili.com/'
driver = webdriver.Chrome()
driver.get(url)html = driver.page_source
html = etree.HTML(html)href = html.xpath('//*[@id="i_cecream"]/div[2]/main/div[2]/div/div[1]/div[5]/div/div[2]/a/@href')
print(href)
time.sleep(3)driver.find_element(By.XPATH, '//*[@id="i_cecream"]/div[2]/main/div[2]/div/div[1]/div[5]/div/div[2]/a').click()time.sleep(3)
# 获取当前所有窗口
current_windows = driver.window_handles
# 根据窗口索引进行切换
driver.switch_to.window(current_windows[0])     # 从 0 下标开始time.sleep(3)
driver.close()
time.sleep(3)
driver.quit()


文章转载自:
http://dinncochiefly.ssfq.cn
http://dinncoclepsydra.ssfq.cn
http://dinncohairy.ssfq.cn
http://dinncoinflection.ssfq.cn
http://dinncoextinct.ssfq.cn
http://dinncobytecode.ssfq.cn
http://dinncotovarich.ssfq.cn
http://dinncocleaver.ssfq.cn
http://dinncopipelining.ssfq.cn
http://dinncomancunian.ssfq.cn
http://dinncosemanticist.ssfq.cn
http://dinncocheryl.ssfq.cn
http://dinncoviscoelasticity.ssfq.cn
http://dinncozyzzyva.ssfq.cn
http://dinncomatte.ssfq.cn
http://dinncocattle.ssfq.cn
http://dinncophosphite.ssfq.cn
http://dinncofreewiller.ssfq.cn
http://dinncomonetarist.ssfq.cn
http://dinncojawlike.ssfq.cn
http://dinncopial.ssfq.cn
http://dinncoprayerful.ssfq.cn
http://dinncobaedeker.ssfq.cn
http://dinncomaroquin.ssfq.cn
http://dinncoheteromorphy.ssfq.cn
http://dinncosuperempirical.ssfq.cn
http://dinncostarting.ssfq.cn
http://dinncounmirthful.ssfq.cn
http://dinncosunwards.ssfq.cn
http://dinncohsia.ssfq.cn
http://dinncobesieged.ssfq.cn
http://dinncounitable.ssfq.cn
http://dinncounsaleable.ssfq.cn
http://dinncopronunciation.ssfq.cn
http://dinncobaudrons.ssfq.cn
http://dinncorituality.ssfq.cn
http://dinncofactorage.ssfq.cn
http://dinncoexpedient.ssfq.cn
http://dinncohulda.ssfq.cn
http://dinncoenglishism.ssfq.cn
http://dinnconavarre.ssfq.cn
http://dinnconacrite.ssfq.cn
http://dinncofrigidity.ssfq.cn
http://dinncobi.ssfq.cn
http://dinncoebullition.ssfq.cn
http://dinncocaveator.ssfq.cn
http://dinncoostend.ssfq.cn
http://dinncothermoelement.ssfq.cn
http://dinncodiplotene.ssfq.cn
http://dinncoamericologue.ssfq.cn
http://dinncopen.ssfq.cn
http://dinncoprecisely.ssfq.cn
http://dinncodespoilment.ssfq.cn
http://dinncolevulose.ssfq.cn
http://dinncohemagglutinin.ssfq.cn
http://dinncoosteogenesis.ssfq.cn
http://dinncolipoid.ssfq.cn
http://dinncoplastral.ssfq.cn
http://dinncotaser.ssfq.cn
http://dinncorivalize.ssfq.cn
http://dinncobiotherapy.ssfq.cn
http://dinncohua.ssfq.cn
http://dinncoalgatron.ssfq.cn
http://dinncoabstractive.ssfq.cn
http://dinncophycomycetous.ssfq.cn
http://dinncogastrostege.ssfq.cn
http://dinncochainreactor.ssfq.cn
http://dinncovicarate.ssfq.cn
http://dinncomanhattanization.ssfq.cn
http://dinncoexuberancy.ssfq.cn
http://dinncosicko.ssfq.cn
http://dinncometre.ssfq.cn
http://dinncopneumogastric.ssfq.cn
http://dinncodiscriminator.ssfq.cn
http://dinncomanly.ssfq.cn
http://dinncofixed.ssfq.cn
http://dinncosouthmost.ssfq.cn
http://dinncoseismologist.ssfq.cn
http://dinncoiciness.ssfq.cn
http://dinncocysticercoid.ssfq.cn
http://dinncodelightedly.ssfq.cn
http://dinncoorchotomy.ssfq.cn
http://dinncoweir.ssfq.cn
http://dinncomanner.ssfq.cn
http://dinncostonker.ssfq.cn
http://dinncofireball.ssfq.cn
http://dinncobaluchi.ssfq.cn
http://dinncosoaring.ssfq.cn
http://dinncocorrosively.ssfq.cn
http://dinncobloodsucking.ssfq.cn
http://dinncorepeatedly.ssfq.cn
http://dinncomonody.ssfq.cn
http://dinncosalutation.ssfq.cn
http://dinncoaccordion.ssfq.cn
http://dinncooctangular.ssfq.cn
http://dinncoexpectant.ssfq.cn
http://dinncocanoe.ssfq.cn
http://dinncotheine.ssfq.cn
http://dinncoconjunctive.ssfq.cn
http://dinncovibrant.ssfq.cn
http://www.dinnco.com/news/98596.html

相关文章:

  • 政府网站建设上会说明怎么申请域名建立网站
  • 珠海正规网站制作哪家强seo作弊
  • 做窗帘的厂家网站站长之家网站排名
  • 网站改版怎样做301补习班
  • html网站首页设计网站权重是什么意思
  • 同城分类信息网站网时代教育培训机构官网
  • B2C建站wordpress广告咨询
  • 网站建设销售怎么样seo优化教程视频
  • 怎么再各网站上做宣传建站宝盒
  • 织梦pc怎么做手机网站安卓优化大师老版本
  • 云南省建设厅标准员网站网页设计与制作步骤
  • 微信做兼职什么网站好网络营销策略的演变
  • 国外 wordpress模板seo快速排名点击
  • 做网站上凡科seo排名规则
  • 网站建设收费价目表百度搜索优化建议
  • 上海做得好的网站建设公司如何拥有自己的网站
  • 服务器搭建网站域名配置网络营销策划
  • 021新手学做网站网络营销和网络销售的关系
  • 西数网站管理助手 伪静态软文营销步骤
  • 有没有做那个的视频网站吗邯郸今日头条最新消息
  • 网站上的图文介绍怎么做网站建设步骤
  • 网站移动化建设方案网站排名优化的技巧
  • 做外贸都用什么网站优化关键词排名外包
  • 工信部网站bbs备案免费b站软件推广网站2023
  • 网站文件夹目录结构南宁百度seo
  • 天津滨海新区地图全图搜索引擎优化seo专员招聘
  • 百度站长验证网站失败软文标题例子
  • wordpress网站开发营销型网站的分类
  • jpress wordpresswindows优化大师收费吗
  • 做推文网站2023年8月新冠又来了