Selenium填坑笔记

1. IE没法正常启动javascript

  1)Security 三个设置成同样的,包括是否启用保护模式也要同样css

  2)页面缩放比例要设置为100%html

2.Firefox启动后为中文环境,但但愿为英文环境java

  Firefox driver每次会启动一个彻底干净的浏览器,profile为空的。可更改profile为当前profile,这样不只使用的就是当前default的环境,同时也就可以启动plug-innode

  profile = webdriver.FirefoxProfile(r'C:\Users\Samsung\AppData\Roaming\Mozilla\Firefox\Profiles\xpttkqws.default-1427164221316')
      self.browser = webdriver.Firefox(profile)jquery

3.webElement点击没有响应web

  使用js模拟点击浏览器

  compose = self.find_element(self.user_browser, *self.__menu_Compose_loc)
        self.user_browser.execute_script('arguments[0].click();', compose)测试

4.经过Send_keys()实现文件上传,Chrome正常但,IE只弹出选择文件对话框ui

  将IEdriver更新为2.53.1正常了,2.53.0出现问题,但以前的老版本也没有问题。经此IEDriver各个版本之间仍是有必定差别

  好比:

    老版本(已不能确认版本号大概是15年版本)中须要self.user_browser.execute_script("arguments[0].style.opacity='1';", input_attach),以后才能对元素执行send_keys()要否则报错

    ElementNotVisibleException: Message: Element is not displayed

    新版本不须要

 5. Firefox在48版本以前不须要使用单独的webdriver, 但须要禁止Firefox的自动更新功能,不然Firefox更新后将致使浏览器不能正常启动。

6. Debug是想要输出html标签内容 <span>Compose</span>, 使用webelement.get_attribute("innerText")  输出Compose.

7. 使用HTMLRunner输出report

    def run(self,testSuite = None): name_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S") file_name = self.report_dir + str(name_time) +"_result.html" fp = open(file_name, "wb") runner = HTMLTestRunner(stream=fp, title="TestResult", description="Test result of compose mail: ") print "**knoxPortal Msg > testControl > run : Run start.." runner.run(testSuite) fp.close() print "**knoxPortal Msg > testControl > run : Run successfully.."

 8. 使用WebElement.findElement(By by) 是查找element的子元素,可是使用xpath时却会查找全局

List<WebElement> findElements(By by) Find all elements within the current context using the given mechanism.
When using xpath be aware that webdriver follows standard conventions:
a search prefixed with
"//" will search the entire document, not just the children of this current node.

Use ".//" to limit your search to the children of this WebElement.
This method is affected by the 'implicit wait' times in force at the time of execution.
When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.

 9.xpath查找不包含某个子元素的当前元素

<li id="lpm_40611_0">
    <a>
        <ul class="prtit_area clear_both">
                <li class="date">当天购</li>
        </ul>
    </a>
    <p class="btn_cart">
        <a>购物车</a>
    </p>
</li>
<li id="lpm_40611_1">
    <a>
        <ul class="prtit_area clear_both">
                <li class="new">新品</li>
        </ul>
    </a>
    <p class="btn_cart">
        <a>购物车</a>
    </p>
</li>
<li id="lpm_40611_2">
    <a>
        <ul class="prtit_area clear_both">
                <li class="new">新品</li>
        </ul>
    </a>
    <p class="btn_cart">
        <a>到货通知</a>
    </p>
</li>
<li id="lpm_40611_3">
    <a>
        <ul class="prtit_area clear_both"> ::after </ul>
    </a>
    <p class="btn_cart">
        <a>当天购</a>
    </p>
</li>

如今须要查找没有分析当天购标签,可是有购物车标签的购物车元素

//ul[@class='prtit_area clear_both' and not(*[@class='date'])]//ancestor::li[@id]//a[contains(text(),'购物车')]

//ul[@class='prtit_area clear_both' and (not(li[@class='date']) or not(li))]//ancestor::li[@id]//a[contains(text(),'购物车')]

对于以上两个的区别not(li[@class="date"])会查找有li标签可是class不为date的ul标签

 

10. selenium2library在使用JavaScript时是使用关键字 execute JavaScript ,这个关键字只支持输入一个纯javascript的list,而底层的selenium库中咱们在使用的时候更经常使用的是使用executejavascript("arguments[0].ckicl();",weblement),因此我么须要本身封装一下这个关键字

#原关键字
def
execute_javascript(self, *code): js = self._get_javascript_to_execute(''.join(code)) self._info("Executing JavaScript:\n%s" % js) return self._current_browser().execute_script(js)

#自定义关键字
def execute_javascript_with_args(self, code,*args):
  return self._current_browser().execute_script(code,*args)
  固然selenium2library其实给出了一种解决方法,就是使用关键字assign Id to element 给element设置一个id值,这样javascript就可以很好的找到元素了。document.findelementbyid("myid ").click(); 或者使用 jquery,  $("#myid").click();
  这里须要使用到jquery的语法,因此须要注意jquery是可以使用css样式进行定位的语法,因此在$("")的括号中,只可使用css的locator 例如$("#id") $(".class") 或者其余复杂的语法结构
 
11.  在web自动化测试过程当中常常遇到有些元素的value或者text值是经过js动态生成的,好比说日期输入框采用日期控件。这个时候可能selenium自己的gettext(), getvalue()方法都不能正确获取到值,这个时候可使用js去解决,$("Id").val() 可以获取到value值,$("Id").val("setvalue")可以set值,$("id").text()可以获取text
 
12.  Javascript还常常用在点击问题上,由于selenium的 点击其实是点击元素所在位置的中心点像素,因此当元素被遮挡时click()可能不报错,可是也没有生效。这个时候最有效的解决方案就是使用js去点击。
  在进行兼容性测试的时候发现win10上一些点击事件会不生效,使用js点击可以解决大部分问题。
  But, js点击并非万能的,在某些特定状况下,使用js点击以后触发所点击元素的onlick()事件,而页面上其余一些相关的事件不能正确触发(eg:点击了邮件中的SentBox按钮,邮件列表加载了SentBox中的邮件,可是SentBox所在的div并无显示为选中状态)因此把全部点击事件都写成js并非一个好的选择,优先使用原生的点击事件,当原生点击事件不生效时再考虑使用js点击。
  须要使用js点击的除了遮盖外,元素在viewport以外(例如存在滚动条,只有滑动滚动条元素才能可见)也是常使用的场景。不过这种在viewpoint以外使用原生的点击之时是会报错的(element is not clickable)。固然除了使用js点击外,此时还可使用js滚动滚动条来使元素可见后点击
#Java 
public void scrollToView(WebElement e) throws Exception {   
    executeJS("window.scrollTo(0," + e.getLocation().y + ")");
    executeJS("arguments[0].scrollIntoView(true);", e);
}
相关文章
相关标签/搜索