Switch case and dropdown in selenium



  • enter image description here

    I am writing code to get state value from the drop-down and depending upon the state calculating the tax rate. I am using switch case statement to define the tax rate for each state. I want to use selected state in the switch case statement, how I can do it.

    Here is my code:

    Select stateSelect = new Select(driver.findElement(By.id("ddlState")));    
    stateSelect.selectByVisibleText(state);
    
    double dl; 
    double taxrate =0; 
    switch(*****) // What I can write here inorder to get selected state
    { 
        case "British Columbia":
             taxrate = 0.05;
             break; 
    
        case "Alberta":
             taxrate = 0.10;
             break; 
            // And so on.....
    } 
    taxprice= pprice*taxrate; 
    


  • 'state' variable holds the name of the state you are about to select. Simply use this variable in switch statement.

    switch(state){
      //cases
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2