Selenium Webdriver - 在 If 语句中使用 isDisplayed() 不起作用 [英] Selenium Webdriver - using isDisplayed() in If statement is not working

查看:46
本文介绍了Selenium Webdriver - 在 If 语句中使用 isDisplayed() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个涉及搜索记录然后更新记录的脚本.在搜索屏幕上,用户可以选择查看高级搜索选项.切换显示或隐藏高级搜索由一键控制.

I am creating a script that involved searching for a record and then updating the record. On the search screen, the user has the option of viewing advanced search options. To toggle showing or hiding advanced search is controlled by one button.

<a title="Searches" href="javascript:expandFilters()"><img border="0" align="absmiddle" alt="Advanced" src="****MASKED URL****"></a>

显示或隐藏高级搜索时搜索按钮的属性之间的唯一区别是img src:

The only difference between the properties of the search button when it is showing or hiding the advanced search is the img src:

当高级搜索隐藏时,IMG src 以/Styles/_Images/advanced_button.jpg"结尾,当高级搜索可见时,IMG src 以/Styles/_Images/basic_button.png"结尾

When advanced search is hidden the IMG src ends with "/Styles/_Images/advanced_button.jpg", when advanced search is visible, the IMG src ends with "/Styles/_Images/basic_button.png"

当我打开页面时,有时会显示高级搜索选项,有时则不会.我要搜索的值出现在高级"部分,因此为了让我的脚本正常工作,我添加了一个 IF 语句.

When I open the page, sometimes the Advanced search options are showing, sometimes they aren't. The value that I want to search on appears in the Advanced section, so for my script to work I have added an IF statement.

<input type="text" value="" maxlength="30" size="30" name="guiSystemID">

IF 语句查找我需要输入数据的字段,如果该字段不存在,则表示高级选项不可见,我需要单击按钮以展开搜索选项.

The IF statement looks for the fields that I need to enter data into, and if the field does not exist then that would indicate that the Advanced options are not visible I need to click on the button to expand the search option.

我创建了以下 IF 语句.

I created the following IF statement.

     if (!driver.findElement(By.name("guiSystemID")).isDisplayed()) {
         driver.findElement(By.cssSelector("img[alt='Advanced']")).click();
     }

当我运行脚本并展开高级搜索时,脚本成功运行.但是,当我运行脚本并且高级搜索未展开时,脚本失败,提示我找不到对象guiSystemID".这令人沮丧,因为如果找不到它,那么我希望脚本继续,进入 IF 语句的真实路径.

When I run the script and the Advanced search is expanded then the script runs successfully. However, when I run the script and the Advanced search is not expanded, the script fails, advising me that it could not find the object "guiSystemID". This is frustrating because if it can't find it then I want the script to continue, entering into the True path of the IF statement.

有没有人对我如何评估该字段是否出现有任何建议,而脚本不会因为找不到该字段而失败.

Has anyone got any suggestions about how else I could assess if the field is appearing without having the script fail because it can't find the field.

提前致谢

西蒙

推荐答案

我可能会迟到回答这个问题,但它可能会帮助其他寻找相同问题的人.

I might be late in answering this, but it might help someone else looking for the same.

我最近在使用 isDisplayed() 时遇到了类似的问题.我的代码是这样的

I recently faced a similar problem while working with isDisplayed(). My code was something like this

if(driver.findElement(By.xpath(noRecordId)).isDisplayed() )                                                                                                         
{         
  /**Do this*/     
}    
else    
{     
  /**Do this*/    
}

当 isDisplayed 尝试查找的元素存在时,此代码运行良好.但是当元素不存在时,它会继续寻找它并因此抛出异常NosuchElementFound".所以我无法测试 else 部分.

This code works pretty well when the element that isDisplayed is trying to find is present. But when the element is absent, it continues looking for that and hence throws an exception "NosuchElementFound". So there was no way that I could test the else part.

我想出了一个方法来处理这个(用 try 和 catch 块包围 {if, else},说这样的话.

I figured out a way to work with this(Surround the {if, else} with try and catch block, say something like this.

public void deleteSubVar() throws Exception  
{         
  try   
  {    
    if(driver.findElement(By.xpath(noRecordId)).isDisplayed() )     
    {      
      /**when the element is found do this*/     
    }    
  }      
  catch(Exception e)     
  {       
   /**include the else part here*/     
  }       
}         

希望这有帮助:)

这篇关于Selenium Webdriver - 在 If 语句中使用 isDisplayed() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆