首页 > 解决方案 > 无法从 Java 中的属性文件中为 Selenium 中的 Select 加载定位器

问题描述

我已经在 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+mobiles31%7C2dMobile 122b-4589-8c70-13b380fd1cfd&as-searchtext=三星

标签: javaseleniumtestng

解决方案


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

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

推荐阅读