Question on driver.findElement(By.cssSelector ... for dropdown | Selenium Forum
M
Monal Posted on 08/05/2020

I have countryDropdownElm; element in my Page…class also the selectors are all defined in my Constant class

In Constantclass...

String COUNTRY_DROPDOWN_LOCATOR = "select#country";

 

Below is the code in my Page class:

@FindBy(css = PartnersContractConstant.COUNTRY_DROPDOWN_LOCATOR)

private WebElement countryDropdownElm;  <— Setting the WebElement

...

Further down I have code to select something from DropDown option:

public PartnerContractPage selectCountryDropDown(String country) {

      Select countryValue= new Select(driver.findElement(By.cssSelector("select#country"))); <—This works

      WebDriverWait wait = new WebDriverWait(driver, 30);

      wait.until(ExpectedConditions.elementToBeClickable(countryDropdownElm));

      countryValue.selectByValue(country);

      return new PartnerContractPage(driver);

}

 

BUT This does not work, gives the below error

org.openqa.selenium.InvalidSelectorException: Given css selector expression "[[FirefoxDriver: firefox on MAC (8fcb734e-b282-5840-8c9c-1ae9401fc1f2)] -> css selector: select#country]" is invalid:

 

public PartnerContractPage selectCountryDropDown(String country) {

Select countryValue= new Select(driver.findElement(By.cssSelector(countryDropdownElm.toString()))); <—Does not work

     WebDriverWait wait = new WebDriverWait(driver, 30);

     wait.until(ExpectedConditions.elementToBeClickable(countryDropdownElm));

     countryValue.selectByValue(country);

     return new PartnerContractPage(driver);

}

 

Sir is there way I could fix it because in By.cssSelector(...) it asking for String as parameter, but when I tried toString() it fails

Can you please help or advice ?

-Monal


M
Monal Replied on 08/05/2020

NVM Instructor I have figured this out

What I did is to do a access to the "COUNTRY_DROPDOWN_LOCATOR" constant in my function since that is what contains the string that I want ("select#country").

 

So Select countryValue= new Select(driver.findElement(By.cssSelector(SomeClassConstant.COUNTRY_DROPDOWN_LOCATOR))); <-- This worked fine with me

-Moanal --- Please close this question 


A
Ashish Thakur Replied on 08/05/2020

ok


Related Posts