【问题标题】:Select an option from a dependent drop box Selenium web driver从依赖下拉框中选择一个选项 Selenium Web 驱动程序
【发布时间】:2014-06-06 04:27:29
【问题描述】:

我的应用程序必须在 IE 中使用。我正在自动化测试,其中脚本首先必须在第一个下拉框“类别”中选择一个选项,以便在第二个下拉框“名称”中显示与类别相关的选项。然后脚本在“名称”中选择一个选项,并显示相关页面。
在“类别”中进行选择之前,“名称”没有任何选项。 HTML 源代码:

<select id="drop_Category">
   <option value =""/>
   <option value = "Category1">
     Text - Category1
   <option value = "Category2">
     Text - Category2
<select id="drop_Name">

选择“Category1”选项后,HTML 源代码变为:

<select id="drop_Category">
   <option value =""/>
   <option value = "Category1">
     Text - Category1
   <option value = "Category2">
     Text - Category2
<select id="drop_Name">
   <option value =""/>
   <option value = "C1_Name1">
     Text - C1_Name1
   <option value = "C1_Name2">
     Text - C1_Name2
   <option value = "C1_Name3">
     Text - C1_Name3

对于选择“Category1”和“C1_Name3”的脚本,我的第一个版本代码是:

//Select option in drop-box "Category"
stringText = "Category1";
var dropCategory = new SelectElement(driver.FindElement(By.Id("drop_Category")));
dropCategory.SelectByText(stringText);

//Select option in drop-box "Name"
stringText = "C1_Name3";
var dropName = new SelectElement(driver.FindElement(By.Id("drop_Name")));
dropName.SelectByText(stringText);

此代码不起作用,因为“名称”中的列表尚未加载,脚本找不到带有文本“C1_Name3”的选项,所以我添加了隐式等待。等待没有帮助,所以我试图捕捉异常。这是第二版代码:

//Select option in drop-box "Category"
stringText = "Category1";
var dropCategory = new SelectElement(driver.FindElement(By.Id("drop_Category")));
dropCategory.SelectByText(stringText);

//Select option in drop-box "Name"
stringText = "C1_Name3"
try
{
   var dropName = new SelectElement(driver.FindElement(By.Id("drop_Name")));
   dropName.SelectByText(stringText);   
}
catch (NoSuchElementException)
{
   var dropName = new SelectElement(driver.FindElement(By.Id("drop_Name")));
   dropName.SelectByText(stringText);   
}

它工作但有时由于 InvalidSelectorException 或 StaleElementReferenceException 异常仍然崩溃。我不知道该怎么做才能始终如一地完成这项工作。另外,我是该领域的新手,所以我不确定在我的第二个版本中编写代码是否是不好的做法。非常感谢任何帮助。

【问题讨论】:

  • 如果元素需要时间才能出现并可用,为什么不使用显式等待?

标签: c# internet-explorer selenium driver


【解决方案1】:

初步方法

通常(根据我的经验)除了等待时间方法外,还需要睡眠时间;所以尝试将两者结合起来。

例如,我会尝试循环直到找到您想要的元素。 伪代码如下:

boolean found = false;
while (!found) {
try
{
   var dropName = new SelectElement(driver.FindElement(By.Id("drop_Name")));
   dropName.SelectByText(stringText);
   found = true;  
}
catch (NoSuchElementException)
{
   var dropName = new SelectElement(driver.FindElement(By.Id("drop_Name")));
   dropName.SelectByText(stringText); 
   //do a short sleep here e.g. 500ms depending on the speed of your site  
}

}

documentation on the InvalidSelectorException 开始,当“用于查找元素的选择器未返回 WebElement”时也会引发该异常。所以捕获 NoSuchElementException 就足够了。

替代方法

  • 根据您的问题,&lt;select id="drop_Name"&gt; 从一开始就存在于代码中。
  • 因此driver.findElement总是通过 ID drop_Name 找到 WebElement
  • 不同之处在于直到您选择Category1drop_Name 没有&lt;option&gt; 值。
  • 因此您可以尝试以下等待功能:

(注意:代码是Java;可以很容易地移植到C#)

private static void waitUntilOptionsLoad() {
    while(true) {
        Thread.sleep(1000);
        List<WebElement> options = driver.findElement(By.id("drop_Name"))
                        .findElements(By.tagName("option"));
        if (options.size() > 0 ) { 
            System.out.println("More than one option tag found; therefore options have loaded");
            break;
        }
}

【讨论】:

  • 谢谢。我结合了两种方法:首先使用等待功能确保“名称”列表加载,然后尝试在“名称”中选择并捕获任何其他异常(没有“try..catch”,有时会出现错误“元素不再有效的”)。有用。我有 20 多个此类测试的循环运行良好。
猜你喜欢
  • 1970-01-01
  • 2020-08-11
  • 2018-01-18
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 2023-04-11
相关资源
最近更新 更多