【问题标题】:Not able to load locator for Select in Selenium from properties file in java无法从 java 中的属性文件中为 Selenium 中的 Select 加载定位器
【发布时间】:2021-11-29 09:10:51
【问题描述】:

我已经在 java 属性文件中外部化了我的定位器。所有其他定位器工作正常,但选择定位器不工作。 我已经通过分配给字符串来检查打印它。它的打印但在 Select 语句中使用时不起作用。 当我在 Select 语句中对定位器进行硬编码时,它可以工作,但不适用于属性文件。

 @Test(priority=2)
public void filter() {
String locator=prop.getProperty("price_dropdown");
System.out.println(locator);
WebElement price=driver.findElement(By.xpath(locator));
Select select=new Select(price);
try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String value=prop.getProperty("price");
    select.selectByValue(value);

try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     
 
driver.findElement(By.xpath(prop.getProperty("ram_filter"))).click();
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     
driver.findElement(By.xpath(prop.getProperty("processor"))).click();
     

driver.findElement(By.xpath(prop.getProperty("snapdragon"))).click(); }

URL-https://www.flipkart.com/search?q=samsung+mobiles&sid=tyy%2C4io&as=on&as-show=on&otracker=AS_QueryStore_OrganicAutoSuggest_1_7_na_na_na&otracker1=AS_QueryStore_OrganicAutoSuggest_1_7_na_na_na&as-pos=1&as-type=RECENT&suggestionId=samsung+mobiles%7 =37316e2d-122b-4589-8c70-13b380fd1cfd&as-searchtext=三星

【问题讨论】:

  • 请在您的问题中添加代码和错误?
  • 如果您无法共享 URL,您还需要共享相同的 HTML 代码。
  • 您遇到的错误是什么?

标签: java selenium testng


【解决方案1】:

请将 prop 声明为全局变量,并在 before suite 方法中添加加载属性逻辑。否则你将面临空指针异常错误。

@BeforeSuite(alwaysRun = true)
public void logerere(){
    BasicConfigurator.configure();
    PropertyConfigurator.configure("Inputs/Log.Properties");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2016-05-18
    相关资源
    最近更新 更多