-1

https://demo.guru99.com/test/newtours/register.php

I was trying to automate the drop down country menu on this page but am unable to select any value from the drop down.

code used

Eclipse is throwing the following error:

Exception in thread "main" java.lang.NoSuchMethodError: 'java.lang.String org.openqa.selenium.WebElement.getDomAttribute(java.lang.String)' at org.openqa.selenium.support.ui.Select.(Select.java:54) at ui.DropDown1.main(DropDown1.java:23)

I can click on the drop down menu but it does not select any value. neither does it print the values of drop down menu stored in List<WebElements>.

3 Answers 3

0

The way you have created the Select object will need change

Select countrySel=new Select(driver.findElement(By.name("country"))); countrySel.selectByValue("Antarctica");

i hope this should help.

0

Looking to the problem statement, I think we have 2 different requirements here:

  1. Print out List containing values.
  2. Fetch value = "ANTARCTICA" from country drop down.

// Code

package TestScripts;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class selectValue {
    
    //Object declaration
    public static FirefoxDriver ffdr = null;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        
        // Creating instance of Firefox Driver
        ffdr = new FirefoxDriver();
        
        // to open given URL using firefox driver with get non static method
        ffdr.get("https://demo.guru99.com/test/newtours/register.php");
        
        System.out.println("Inside BeforeClass");
        
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        
        System.out.println("Inside AfterClass end of program.");
        //quits the entire browser session with all its tabs and windows
        ffdr.quit();
        
    }

    @Test
    public void test() {
        
        
        /*To print out list you can do it in this way.
         * */
        List<WebElement> drop = ffdr.findElements(By.xpath("//select[@name='country']"));
        System.out.println(drop.get(0).getText());
        
        /*To select value we can easily identify by this way by name.
         * */
        WebElement cntry = ffdr.findElement(By.name("country"));
        Select drpcntry = new Select(cntry);
        drpcntry.selectByValue("ANTARCTICA");
        
        /*
        String[] words=drop.get(0).getText().toString().split("\\n");//splits the string based on New line  
        //using java for each loop to print elements of string array  
        for(String w:words){  
            System.out.println(w);
        } */ 
                
    }
}

I hope, this satisfied your expectations. Thanks.

0

This scenario is faced by every qa person in automation testing services company.

Firstly, As your dropdown tag is Select, you can select value directly by using selectByValue, selectByVisibleText like below without clicking the dropdown:

Select s = new Select(driver.findElement(By.name("country")));

s.selectByValue("Antarctica");

Secondly for capturing the dropdown values use below code:

  // getting the list of options in the dropdown with getOptions()
  List <WebElement> op = s.getOptions();
  int size = op.size();
  for(int i =0; i<size ; i++){
     String options = op.get(i).getText();
     System.out.println(options);
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.