Unable to select Bootstrap dropdown in Java Selenium Unable to select Bootstrap dropdown in Java Selenium selenium selenium

Unable to select Bootstrap dropdown in Java Selenium


Looking at the HTML you have provided, the WebElement with id="lead_field_import_email_address" is with in a Select tag. So instead of using Actions Class in this case we should try to use the Select Class instead as follows:

WebElement element =  driver.findElement(By.id("lead_field_import_email_address"));Select select = new Select(element);

Next, we can select any of the options by invoking either selectByIndex(n), selectByValue("value") or selectByVisibleText("visible_text") method.


I tried to replicate manually the steps of your code but when I load the data_file.csv and I click the Upload button:

enter image description here

nothing happens.

And, in the html, there isn't the element that you try to find:

WebElement dropdownToggle = driver.findElement(By.xpath("id('lead_field_import_email_address')"));

That's why I wrote this comment.

I can imagine that should open something. From the informations:

  • Limit
  • Delimiter
  • Enclosure
  • Escape

I can suppose that you have to upload a file with a particular format.

EDIT

Trying locally it works.I don't understand well this part of your code:

        //Select from dropdown        WebElement dropdownToggle = driver.findElement(By.xpath("id('lead_field_import_email_address')"));        Actions cursor = new Actions(driver);        cursor.moveToElement(dropdownToggle);        cursor.click();        cursor.perform();        Thread.sleep(1000);        WebElement weh = driver.findElement(By.id("lead_field_import_email_address_chosen"));        Actions cursor2 = new Actions(driver);        cursor2.moveToElement(weh);        cursor2.click();

If you want to choose from the email drop-down, you could use the xpath:

WebElement we = driver.findElement(By.xpath("//div[@class='choice-wrapper']//div[@id='lead_field_import_email_address_chosen']"));

or simply the id:

WebElement we= driver.findElement(By.id("lead_field_import_email_address_chosen"));

and execute the interested operation.

So, for example:

    WebElement we= driver.findElement(By.id("lead_field_import_email_address_chosen"));    we.click();    we.sendKeys("email");    we.sendKeys(Keys.ENTER);

EDIT 2

Your problem is that you use Thread.sleep(XXXX); in order to wait the elements. This is not deterministic. You must use an explicit wait.

From Explicit Waits:

An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. The extreme case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

So, in java you need:

WebDriverWait wait = new WebDriverWait(driver, 15);WebElement we=wait.until(ExpectedConditions.elementToBeClickable(By.id("lead_field_import_email_address_chosen")));

Always from the previous link:

This waits up to 15 seconds before throwing a TimeoutException unless it finds the element to return within 15 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

Finally, the entire code:

        String geckoDriver = System.getProperty("pathTo/geckodriver";        System.setProperty("webdriver.gecko.driver", geckoDriver);        WebDriver driver= new FirefoxDriver();        driver.get("http://localhost:8888/2.10.0/s/contacts/import/new");        driver.findElement(By.id("username")).sendKeys("admin");        driver.findElement(By.id("password")).sendKeys("password");        driver.findElement(By.className("btn")).click();        WebDriverWait wait = new WebDriverWait(driver, 15);        WebElement uploadBox=wait.until(ExpectedConditions.elementToBeClickable(By.id("lead_import_file")));        uploadBox.sendKeys("/pathTo/data_file.csv");        driver.findElement(By.id("lead_import_start")).click();        //Select from dropdown        WebElement we=wait.until(ExpectedConditions.elementToBeClickable(By.id("lead_field_import_email_address_chosen")));        we.click();        we.sendKeys("email");        we.sendKeys(Keys.ENTER);

EDIT 3

With Firefox, the above code (in my tests) is ok. I noticed that you use Chrome. With Chrome, I have this problem. The solution works for me:

        //Select from dropdown        WebElement we=wait.until(ExpectedConditions.elementToBeClickable(By.id("lead_field_import_email_address_chosen")));        /*we.click();        we.sendKeys("email");*/        Actions actions = new Actions(driver);        actions.moveToElement(we);        actions.click();        actions.sendKeys("email");        actions.sendKeys(Keys.ENTER);        actions.build().perform();