【问题标题】:How can I get the drop down option from the Property file- Selenium WebDriver?如何从属性文件 Selenium WebDriver 中获取下拉选项?
【发布时间】:2014-02-13 05:48:06
【问题描述】:
  • 目前正在开发 Selenium WebDriver 并使用 JavaTestNG 框架中生成的报告工作。
  • 目前正在为 Web 应用程序编写脚本。它包含许多下拉菜单,每个下拉菜单都包含许多选项。
  • 我有属性文件(以 Login.txt 文件的形式)。因为我已经存储了我的用户 ID 密码和 url,然后会得到 String uid=p.getProperty("loginUsername"); 这样的。
  • 该方案的方式与此相同,它需要通过下拉列表的 ID 查找元素,然后需要从文本文件中获取选项以在下拉列表中进行选择。
  • 请建议我是否有可能像这样工作。

HTML标签如下:

<select id="periodId" name="period" style="display: none;">
<option value="l4w">Last 4 Weeks</option>
<option value="l52w">Last 52 Weeks</option>
<option value="daterange">Date Range</option>
<option value="weekrange">Week Range</option>
<option selected="" value="monthrange">Month Range</option>
<option value="yeartodate">Year To Date</option>
</select>

如何在文本文件以及 selenium webdriver 代码中给出这些值。

【问题讨论】:

    标签: java selenium-webdriver


    【解决方案1】:

    你可以类似下面的代码:

    Select dropdown = new Select(driver.findElement(By.id("designation")));
    // To select its option say 'Programmer' you can do
    dropdown.selectByVisibleText("Programmer ");
    // or
    dropdown.selectByIndex(1);
    // or
    dropdown.selectByValue("prog");
    

    (取自here

    【讨论】:

    • 我猜 OP 想从属性文件中检索值以选择值。
    • 我尝试了以上所有方法仍然无法选择下拉值 :( 当使用 Karna's 方法时,我收到错误 无法定位具有值的选项:过去 52 周,当尝试 Sumitbit2005 得到如下错误 找不到具有空 id 属性的元素。谁能帮我解决这个问题问题..
    • 请查看我正在使用的另一个答案:我使用了 dropdown.selectByVisibleText("value");而不是 dropdown.selectByValue("");
    【解决方案2】:
    // Get dropdown id from properties file and find element  
    WebElement element = driver.findElement(By.id(p.getProperty("dropdown.id")));
    // Make a Select object using element
    Select select = new Select(element);  
    // Get option from properties file and use it
    select.selectByVisibleText(p.getProperty("dropdown.option"));
    

    在属性中,您可以将其存储为
    dropdown.option=Last 52 Weeks

    dropdown.option=Week Range

    【讨论】:

    • 请任何人建议我解决方案
    • @Amir:您还在寻找什么?如果我正确理解了您的问题,您希望从属性文件中获取下拉选项并想要使用它。对吗?
    • 是的。你将如何在属性文件中显示它。是我需要存储的普通 html 格式吗
    • @Amir:您不需要将 HTML 存储在属性文件中。我已经更新了我的答案
    • 我可以用同样的方法在文本文件中给出所有值。如何调用特定选项以从属性文件中进行选择
    【解决方案3】:

    如果你有属性文件:那么你可以这样做

    1. 输入属性

    periodId = 过去 52 周

    并使用

    WebElement periodId = driver.findElement(By.xpath("xpath expression"));
    String periodIdvalue =p.getProperty("periodId ");
    periodId.sendKeys(periodIdvalue );
    

    通过这种方式,您可以使用属性从下拉列表中选择值。

    或者你可以使用

    dropdown.selectByValue(periodIdvalue);
    

    而不是发送密钥

    【讨论】:

      【解决方案4】:

      我已经尝试过上面的答案:我收到错误“无法找到具有值的选项:过去 52 周”所以我使用了

      dropdown.selectByVisibleText(period);
      

      它成功了。

      以下是我的步骤:

      1. 我有 application.properties 文件来列出属性:

        PeriodId=过去 52 周

      2. ReadProperty.java 文件,从 application.properties 读取值:

        包 com.util;

        import java.io.*;
        import java.util.*;
        
        public class ReadProperty
        {                       
                public String readApplicationFile(String key)
                {           
                    String path =  this.getPath();  
                    System.out.println(path);
                    String value = "";
                    try{                
                          Properties prop = new Properties();
                          File f = new File(path + "//src//com//util//application.properties");
                          System.out.println(f.getPath());
                          if(f.exists())
                          {
                              prop.load(new FileInputStream(f));
                              value = prop.getProperty(key);  
                        }
                   }
                    catch(Exception e)
                    {  
                       System.out.println("Failed to read from application.properties file.");  
                    }
                    return value;
                 }      
                public String getPath()
                {
                    String path ="";        
                    File file = new File("");
                    String absolutePathOfFirstFile = file.getAbsolutePath();
                    path = absolutePathOfFirstFile.replaceAll("\\\\+", "/");        
                    return path;
                }   
        
          }
        
      3. 我的选择值的代码:

      package com;
      
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.support.ui.Select;
      import org.testng.annotations.Test;
      
      import com.util.ReadProperty;
      
      
      public class NewTest4 {
        protected WebDriver driver;
        ReadProperty readpr = new ReadProperty();  
      
        @Test
        public void testfile() throws Exception
        {
          driver = new FirefoxDriver();
          driver.get("file:///C:/Users/Sumit.Mittal/Desktop/ff.html");
          String period = readpr.readApplicationFile("PeriodId");
          System.out.println("the value is" + period);
          WebElement element = driver.findElement(By.xpath("//*[@id='periodId']"));
          Select dropdown = new Select(element);
          //dropdown.selectByValue(period);
          dropdown.selectByVisibleText(period);
      
      
        }
        public void killDriver()
        {
        driver.close();  
      
        }
      
      }
      

      试试这个,它会起作用的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        • 2016-10-10
        • 2011-09-19
        • 1970-01-01
        • 2017-09-18
        • 2012-09-02
        相关资源
        最近更新 更多