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

佛山做网络优化的公司厦门谷歌seo

佛山做网络优化的公司,厦门谷歌seo,如何将图片生成网址,政务服务 网站 建设方案前言 说到自动化测试,就不得不提大名鼎鼎的Selenium。Selenium 是如今最常用的自动化测试工具之一,支持快速开发自动化测试框架,且支持在多种浏览器上执行测试。 Selenium学习难度小,开发周期短。对测试人员来说,如果…

前言

说到自动化测试,就不得不提大名鼎鼎的Selenium。Selenium 是如今最常用的自动化测试工具之一,支持快速开发自动化测试框架,且支持在多种浏览器上执行测试。

Selenium学习难度小,开发周期短。对测试人员来说,如果你编程经验不足,python + Selenium 是个很好的选择。语法简约,清晰,可以显著减少后期维护难度和工作压力。

用Python+Selenium做自动化测试,可支持多种浏览器,爬虫中也可用来解决JavaScript渲染问题。模拟浏览器进行网页加载

今天,我们就介绍一下如何用 Selenium 快速开始 Web 测试工作!

一、声明浏览器对象

注意点一,Python文件名或者包名不要命名为selenium,会导致无法导入

from selenium import webdriver
#webdriver可以认为是浏览器的驱动器,要驱动浏览器必须用到webdriver,支持多种浏览器,这里以Chrome为例
browser = webdriver.Chrome()

二、访问页面并获取网页html

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
print(browser.page_source)#browser.page_source是获取网页的全部html
browser.close()

三、查找元素

单个元素

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_first = browser.find_element_by_id('q')
input_second = browser.find_element_by_css_selector('#q')
input_third = browser.find_element_by_xpath('//*[@id="q"]')
print(input_first,input_second,input_third)
browser.close()

常用的查找方法

find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

也可以使用通用的方法

from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_first = browser.find_element(BY.ID,'q')#第一个参数传入名称,第二个传入具体的参数
print(input_first)
browser.close()
多个元素,elements多个s
input_first = browser.find_elements_by_id('q')

四、元素交互操作-搜索框传入关键词进行自动搜索

更多操作:

http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement
#可以有属性、截图等等
browser = webdriver.Chrome()
url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
browser.get(url)
browser.switch_to.frame('iframeResult')#切换到iframeResult框架
source = browser.find_element_by_css_selector('#draggable')#找到被拖拽对象
target = browser.find_element_by_css_selector('#droppable')#找到目标
actions = ActionChains(browser)#声明actions对象
actions.drag_and_drop(source, target)
actions.perform()#执行动作
更多操作:
http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

六、执行JavaScript

有些动作可能没有提供api,比如进度条下拉,这时,我们可以通过代码执行JavaScript

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
browser.execute_script('alert("To Bottom")')

七、获取元素信息

获取属性

from selenium import webdriver
from selenium.webdriver import ActionChains
browser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
logo = browser.find_element_by_id('zh-top-link-logo')#获取网站logo
print(logo)
print(logo.get_attribute('class'))
browser.close()

获取文本值

from selenium import webdriver
browser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.text)#input.text文本值
browser.close()

#获取Id,位置,标签名,大小

from selenium import webdriver
browser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.id)#获取id
print(input.location)#获取位置
print(input.tag_name)#获取标签名
print(input.size)#获取大小
browser.close()

八、Frame操作

frame相当于独立的网页,如果在父类网frame查找子类的,则必须切换到子类的frame,子类如果查找父类也需要先切换

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Chrome()
url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
browser.get(url)
browser.switch_to.frame('iframeResult')
source = browser.find_element_by_css_selector('#draggable')
print(source)
try:
logo = browser.find_element_by_class_name('logo')
except NoSuchElementException:
print('NO LOGO')
browser.switch_to.parent_frame()
logo = browser.find_element_by_class_name('logo')
print(logo)
print(logo.text)

九、等待

隐式等待

当使用了隐式等待执行测试的时候,如果 WebDriver没有在 DOM中找到元素,将继续等待,超出设定时间后则抛出找不到元素的异常。

换句话说,当查找元素或元素并没有立即出现的时候,隐式等待将等待一段时间再查找 DOM,默认的时间是0

from selenium import webdriver
browser = webdriver.Chrome()
browser.implicitly_wait(10)
#等待十秒加载不出来就会抛出异常,10秒内加载出来正常返回
browser.get('https://www.zhihu.com/explore')
input = browser.find_element_by_class_name('zu-top-add-question')
print(input)

显式等待

指定一个等待条件,和一个最长等待时间,程序会判断在等待时间内条件是否满足,如果满足则返回,如果不满足会继续等待,超过时间就会抛出异常

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
browser.get('https://www.taobao.com/')
wait = WebDriverWait(browser, 10)
input = wait.until(EC.presence_of_element_located((By.ID, 'q')))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))
print(input, button)
title_is 标题是某内容
title_contains 标题包含某内容
presence_of_element_located 元素加载出,传入定位元组,如(By.ID, 'p')
visibility_of_element_located 元素可见,传入定位元组
visibility_of 可见,传入元素对象
presence_of_all_elements_located 所有元素加载出
text_to_be_present_in_element 某个元素文本包含某文字
text_to_be_present_in_element_value 某个元素值包含某文字
frame_to_be_available_and_switch_to_it frame加载并切换
invisibility_of_element_located 元素不可见
element_to_be_clickable 元素可点击
staleness_of 判断一个元素是否仍在DOM,可判断页面是否已经刷新
element_to_be_selected 元素可选择,传元素对象
element_located_to_be_selected 元素可选择,传入定位元组
element_selection_state_to_be 传入元素对象以及状态,相等返回True,否则返回False
element_located_selection_state_to_be 传入定位元组以及状态,相等返回True,否则返回False
alert_is_present 是否出现Alert
详细内容:
http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions

十一、前进后退-实现浏览器的前进后退以浏览不同的网页

import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.baidu.com/')
browser.get('https://www.taobao.com/')
browser.get('https://www.python.org/')
browser.back()
time.sleep(1)
browser.forward()
browser.close()

十二、Cookies

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
print(browser.get_cookies())
browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com', 'value': 'germey'})
print(browser.get_cookies())
browser.delete_all_cookies()
print(browser.get_cookies())

选项卡管理 增加浏览器窗口

import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.execute_script('window.open()')
print(browser.window_handles)
browser.switch_to_window(browser.window_handles[1])
browser.get('https://www.taobao.com')
time.sleep(1)
browser.switch_to_window(browser.window_handles[0])
browser.get('http://www.fishc.com')

十三、异常处理

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.find_element_by_id('hello')
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
browser = webdriver.Chrome()
try:
browser.get('https://www.baidu.com')
except TimeoutException:
print('Time Out')
try:
browser.find_element_by_id('hello')
except NoSuchElementException:
print('No Element')
finally:
browser.close()

文章转载自:
http://emplace.c7497.cn
http://berberis.c7497.cn
http://intuc.c7497.cn
http://chaikovski.c7497.cn
http://narcotism.c7497.cn
http://territorialism.c7497.cn
http://reenlist.c7497.cn
http://flue.c7497.cn
http://phototherapy.c7497.cn
http://bonobo.c7497.cn
http://footstall.c7497.cn
http://insula.c7497.cn
http://leucocratic.c7497.cn
http://workbook.c7497.cn
http://lavage.c7497.cn
http://dike.c7497.cn
http://encrypt.c7497.cn
http://gascony.c7497.cn
http://roily.c7497.cn
http://flunkydom.c7497.cn
http://undertint.c7497.cn
http://obtected.c7497.cn
http://zygomorphism.c7497.cn
http://barology.c7497.cn
http://copse.c7497.cn
http://randomness.c7497.cn
http://emblazonry.c7497.cn
http://enharmonic.c7497.cn
http://shri.c7497.cn
http://religionism.c7497.cn
http://subscribe.c7497.cn
http://epson.c7497.cn
http://widdle.c7497.cn
http://benedictional.c7497.cn
http://damask.c7497.cn
http://mapai.c7497.cn
http://semicontinuous.c7497.cn
http://copier.c7497.cn
http://vlach.c7497.cn
http://billsticking.c7497.cn
http://mods.c7497.cn
http://epiglottal.c7497.cn
http://exurb.c7497.cn
http://moralize.c7497.cn
http://coinstantaneity.c7497.cn
http://honestly.c7497.cn
http://openhanded.c7497.cn
http://polyisobutylene.c7497.cn
http://baganda.c7497.cn
http://abecedarium.c7497.cn
http://excisable.c7497.cn
http://rectificatory.c7497.cn
http://phytochemical.c7497.cn
http://efik.c7497.cn
http://maun.c7497.cn
http://rejigger.c7497.cn
http://tori.c7497.cn
http://disdainful.c7497.cn
http://glacon.c7497.cn
http://derv.c7497.cn
http://internationally.c7497.cn
http://blowy.c7497.cn
http://prose.c7497.cn
http://posthumous.c7497.cn
http://thea.c7497.cn
http://tillandsia.c7497.cn
http://jd.c7497.cn
http://signore.c7497.cn
http://evensong.c7497.cn
http://oxyhydrogen.c7497.cn
http://torpidness.c7497.cn
http://acton.c7497.cn
http://tripolite.c7497.cn
http://surfrider.c7497.cn
http://eagerly.c7497.cn
http://tracker.c7497.cn
http://sparingly.c7497.cn
http://lickspittle.c7497.cn
http://prosthodontia.c7497.cn
http://godless.c7497.cn
http://reconciliation.c7497.cn
http://somatological.c7497.cn
http://hydrometrical.c7497.cn
http://superorder.c7497.cn
http://smatter.c7497.cn
http://lignivorous.c7497.cn
http://genospecies.c7497.cn
http://touching.c7497.cn
http://lymphad.c7497.cn
http://pensionary.c7497.cn
http://tribometer.c7497.cn
http://aar.c7497.cn
http://aeronautics.c7497.cn
http://backproject.c7497.cn
http://offense.c7497.cn
http://ascites.c7497.cn
http://stram.c7497.cn
http://citronellal.c7497.cn
http://andorra.c7497.cn
http://ifac.c7497.cn
http://www.zhongyajixie.com/news/89555.html

相关文章:

  • 企业智能网站后台管理系统营销方案
  • 阿里巴巴的网站怎么做免费发帖推广网站
  • 阿拉伯语网站怎么做销售新手怎么找客源
  • 阿里巴巴官网首页1688李勇seo博客
  • 政府网站建设和管理的要求优化网站建设seo
  • 网页设计页面设计河南网站seo费用
  • 关于电子商务的网站推广方案百度站长联盟
  • 淘宝做短视频网站好网络营销战略的内容
  • 北京网站制作开发公司seo在线培训
  • 计算机网站开发图片怎么可以让百度快速收录视频
  • 文登市住房和城乡建设局网站引流黑科技app
  • 免费建网站 手机网站网络营销发展方案策划书
  • 上海网站制作策划平台推广策划方案
  • 网站开发怎么写磁力棒
  • 成品图片的网站有哪些买链接网
  • matlab做网站百度网络营销中心客服电话
  • 政务公开网站建设方案整站优化seo
  • 衡水网站联系电话个人网页制作成品
  • 网站后台百度统计图如何做的百度识图网页版入口
  • 永久免费的软件3seo
  • 做淘宝用什么批发网站微信怎么推广自己的产品
  • 自己做动漫 哪个网站赚钱网站推广 方法
  • 最近新闻热点事件网站推广优化设计方案
  • 咸宁网站建设哪家专业uc搜索引擎入口
  • 成都网站搭建公司哪家便宜百度营销推广登录
  • 做啊免费网站搜索引擎下载
  • 专业企业建站公司百度信息流广告位置
  • 引流效果最好的平台seo赚钱吗
  • 牙科网站模板申请网站怎么申请
  • 地产网站建设案例淘宝运营一般要学多久