多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

那些年在Selenium中踩过的坑-自动化测试

更新时间:2022-04-22 09:15:09 作者:多测师 浏览:234

  1.selenium 在打开firefox后,发现程序‘死’那里了,不动了,后面的代码不执行,最后抛出异常说超时。

  原因:这个主要原因selenium在运行时会在firefox中安装一个Firefox WebDriver的插件,如果firefox版本太高,比如最新的FF48版本,在48版本中,对于安装的插件要进行验证,没有经过验证的插件不会被运行,而且通过在firefox中输入about:config,设置xpinstall.signatures.required为true,同样也无法生效。

  解决方案:很简单,用低版本的firefox把,比如firefox45,当然也请跟踪selenium的开发进度,目前正有个geckodriver的新版本开发过程中,不过当前这个时间点最好的办法是降低你的firefox版本。目前为止47.01是可以用的。

那些年在Selenium中踩过的坑-自动化测试

  上代码便于说清楚,以下代码是正确无任何问题

  #coding=utf-8

  #运行环境配置

  #主要配置firefox的profile文件是否可用

  import os

  import sys

  from selenium import webdriver

  from selenium.common.exceptions import NoSuchElementException

  gourl='http://www.baidu.com/'

  #获得webdriver函数

  def get_webdriver():

  #定制firefox的profile文件

  profileDir = r"d:\xiaoshuo\profile"

  profile1 = webdriver.FirefoxProfile(profileDir)

  #亲们重点关注这句就好,其他更多的不用关心

  br=webdriver.Firefox(profile1)

  br.set_window_size(600,600)

  return br

  br=get_webdriver()

  br.get(gourl)

  2.在使用find_element_by_xxxx()查找元素时,如果元素找不到,不会返回None,而是抛出异常,你必须得自己捕获异常

  from selenium import webdriver

  from selenium.common.exceptions import NoSuchElementException

  br=webdriver.Firefox()

  gourl='http://www.baidu.com/'

  br.get(gourl)

  try:

  xiaoyiye=br.find_element_by_link_text(u'下一页')

  #找到要做的事情

  except NoSuchElementException:

  #找不到异常处理

  print "no next page"

  3.selenium启动firefox,如果不指定profile文件,将只能使用firefox默认配置,无法进行浏览器定制,比如不显示图片,启动广告插件等,你必须得自己配置profile,让selenium用指定配置启动

  from selenium import webdriver

  from selenium.common.exceptions import NoSuchElementException

  profileDir = r"G:\myproject\python\xiaoshuo\profile"

  profile1 = webdriver.FirefoxProfile(profileDir)

  time.sleep(1)

  br=webdriver.Firefox(profile1)

  gourl='http://www.baidu.com'

  br.get(gourl)

  4.在使用firefox的 profile文件后,你会发现很多选项虽然在浏览器中进行了设置但是在通过selenium启动firefox的时候很多设置没有生效,所以你还得必须会通过代码进行配置设置来关闭图片

  profileDir = r"G:\myproject\python\xiaoshuo\profile"

  profile1 = webdriver.FirefoxProfile(profileDir)

  profile1.set_preference('permissions.default.stylesheet', 2)

  profile1.set_preference('permissions.default.image', 2)

  profile1.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')

  br=webdriver.Firefox(profile1)

  gourl='http://www.duzheba.cc/'

  br.get(gourl)

  5. 用标签页代替弹出窗口无法设置成功

  在python使用selenium来操控firefox的时候,有时候希望所有的新开窗口用TabPage来代替,但是如果你以为通过设置firefox的profile文件目录,或者在代码中通过profile1.set_preference('browser.link.open_newwindow',3)来搞定,那么你会发现你启动的窗口永远browser.link.open_newwindow的值永远等于2,也就是“需要新建窗口时以新建标签页代替”选项永远是没有选中的,除非手动点击一下。

  标签页选项

  原因很简单,因为selenium的绑定中已经将这个选项写成了固定值,所以无论你如何设定除非在窗口启动后手动点击,否则该项用户按不会选中。而该问题的解决在于直接手动改写selenium代码中的设置,具体方法如下:

  确定目录,在我的机器上是C:\Python27\Lib\site-packages\selenium\webdriver\firefox,大家可以根据自己的机器情况进行调整。

  编辑器打开目录中的webdriver_prefs.json文件,将browser.link.open_newwindow的值修改为3。

  以上内容为大家介绍了自动化测试中的那些年在Selenium中踩过的坑,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部