简体   繁体   中英

Why the select method is not working in the category field?

My Code:

public class asdadsd {

public static void main(String[] args) throws InterruptedException {

    WebDriver driver = new FirefoxDriver();

    driver.manage().window().maximize();

    driver.get("http://talentrack.in");

    driver.findElement(By.xpath(".//*[@id='header']/div[2]/div[2]/div[2]/a/span")).click();

    WebDriverWait wait = new WebDriverWait(driver, 20);

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='userlogin']/div/div[4]/a[1]")));

    driver.findElement(By.xpath(".//*[@id='userlogin']/div/div[4]/a[1]")).click();


    WebElement name = driver.findElement(By.xpath(".//*[@id='name']"));
    name.sendKeys("anyname");

    //WebDriverWait wait = new WebDriverWait(driver, 20);
      //wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("select[id='cat_id'][name='cat_id']")));

    Thread.sleep(5000L);

    //WebElement category = driver.findElement(By.cssSelector("select[id='cat_id'][name='cat_id']"));

    WebElement category = driver.findElement(By.cssSelector("#cat_id"));

    Select a =new Select(category);

    a.selectByValue("5");
}

}

What is wrong with category drop-down ? I'm able to fill values in other drop-downs. Please Help me in getting rid of this.

Error: Element is not currently visible and so may not be interacted with Command duration or timeout: 13 milliseconds

I applied wait to, still it is not working.

@Kishan,

In your code WebDriver unable to select the dropdown because it found two matching element with your css selector. PFA the screenshot. CSS选择器显示两个节点 So if you want to use css selector then you can use:

#cat_id[class='input-control modal-tab-selection placeholder-color'] instead of #cat_id .

WebElement category = driver.findElement(By.cssSelector("#cat_id[class='input-control modal-tab-selection placeholder-color']"));

Select a =new Select(category);

a.selectByValue("5");

I hope this will help.

    WebElement category = driver.findElement(By.xpath(".//*[@id='cat_id'][@data-message='required']"));
    Select a =new Select(category);
    a.selectByValue("4");

Finally i got it..

This was the xpath which helped me to uniquely identify the dropdown. Thanks Vaibhav for your help. Never trust xpath, better create your own. ha ha.. Happy Learning. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM