zoukankan      html  css  js  c++  java
  • Selenium填坑笔记

    1. IE无法正常启动

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

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

    2.Firefox启动后为中文环境,但希望为英文环境

      Firefox driver每次会启动一个完全干净的浏览器,profile为空的。可更改profile为当前profile,这样不仅使用的就是当前default的环境,同时也就能够启动plug-in

      profile = webdriver.FirefoxProfile(r'C:UsersSamsungAppDataRoamingMozillaFirefoxProfilesxpttkqws.default-1427164221316')
          self.browser = webdriver.Firefox(profile)

    3.webElement点击没有响应

      使用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只弹出选择文件对话框

      将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: %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);
    }
     
     
     

     

  • 相关阅读:
    机器学习基石13-Hazard of Overfitting
    机器学习基石12-Nonlinear Transformation
    机器学习基石11-Linear Models for Classification
    Java基础14-缓冲区字节流;File类
    Java基础13-字符串缓冲流;字节流
    机器学习基石10-Logistic Regression
    机器学习基石9-Linear Regression
    POJ 2245 Lotto dfs
    POJ 1008 Maya Calendar 水模拟
    POJ 1007 DNA Sorting
  • 原文地址:https://www.cnblogs.com/tina-cherish/p/6728775.html
Copyright © 2011-2022 走看看