TA的每日心情 | 衰 2015-4-9 17:10 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]测试小兵
|
本帖最后由 jia8162 于 2012-2-16 13:39 编辑
由于选项框是自动生成的
如果选项一样还用Xpath text去定位的话会出现定位多个的情况发生(在运行的时候就只定位第一个实行)
我的项目中等到的就碰到以下情况
<div id="ext-gen259" class="x-layer x-combo-list" style="position: absolute; z-index: 11000; visibility: hidden; left: -10000px; top: -10000px; width: 148px; height: 42px;">
<div id="ext-gen261" class="x-combo-list-inner" style="width: 148px; overflow: auto; height: 42px;">
<div class="x-combo-list-item x-combo-selected">CCCCC</div>
<div class="x-combo-list-item">AAAAA</div>
</div>
</div>
<div id="ext-gen314" class="x-layer x-combo-list" style="position: absolute; z-index: 11000; visibility: visible; left: 76px; top: 249px; width: 148px; height: 84px;">
<div id="ext-gen316" class="x-combo-list-inner" style="width: 148px; overflow: auto; height: 84px;">
<div class="x-combo-list-item x-combo-selected">CCCCC</div>
<div class="x-combo-list-item">BBBBB</div>
<div class="x-combo-list-item">DDDDD</div>
<div class="x-combo-list-item">AAAAA</div>
</div>
</div>
从上面就可以看出 id、class、都不能用 唯一能用的就是 style
但是 如果在IE中运行的话 style 是不能定位的
那怎么办呢 还好我找到了
getAttribute()的方法
这个方法可以返回Xpath 属性的内容
就可以判断是那个个下拉选项框显示就能定位到唯一的选项了
一下是我的代码- //下拉框选择
- private void objectSelectText(String objectXpath,String SelectText)
- {
- String htmlLocator = "";
- try{
- ifElementTrue(objectXpath.split(";")[0]);
- selenium.click(objectXpath.split(";")[0]);
- //火狐
- if(this.getBrowserType().toLowerCase().equals("firefox3"))
- {
- htmlLocator = "//div[contains(@style,'visibility: visible')]/" + objectXpath.split(";")[1] +"/div[text()='" + SelectText + "']";
- }
- else
- {
- htmlLocator = ConXpach("//div" + "[text()='" + SelectText + "']");
- }
- //
- Thread.sleep(300);
- selenium.mouseOver(htmlLocator);
- Thread.sleep(300);
- selenium.mouseDown(htmlLocator);
- Thread.sleep(300);
- selenium.click(htmlLocator);
- this.logger.info(Handle + "---->" + SelectText + ":PASS");
- FailOrPass = "PASS";
- //ScreenShots();
- }catch(Exception e)
- {
- StackTrace = e.toString();
- FailOrPass = "FAIL";
- ScreenShots("err");
- this.logger.error("Exception",e);
- }
- }
- //IE下拉框判断
- public String ConXpach(String Xpath)
- {
- int sumConList = 0;
- String isXpath = "";
- String runhtmlLocator = "";
- String tmpStyle = "";
- do
- {
- sumConList++;
- isXpath = "//div[contains(@class,'x-layer x-combo-list')]["+String.valueOf(sumConList)+"]";
-
- }while(selenium.isElementPresent(isXpath));
- if(sumConList !=1 && sumConList > 0)
- {
- sumConList=sumConList-1;
- }
-
- for(int index = 1 ; index<=sumConList;index++)
- {
- tmpStyle = selenium.getAttribute("//div[contains(@class,'x-layer x-combo-list')]["+String.valueOf(index)+"]/@style");
- if(tmpStyle.indexOf("visible")>=0)
- {
- runhtmlLocator = "//div[contains(@class,'x-layer x-combo-list')]["+String.valueOf(index)+"]"+Xpath;
- break;
- }
- }
-
- return runhtmlLocator;
- }
复制代码 大家看多以后有什么更好的方法可以回复哦 |
|