Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to select Bootstrap dropdown in Java Selenium

I'm having a very difficult time selecting bootstrap dropdown in Selenium.

I'm new to Selenium so any suggestion and guidance would be very helpful, as what I'd love to do is just select the dropdown, type "email", and press enter during the instance.

I've gone through dozens of solutions, but none of them have worked for this specific issue.

What can I do? Please help.

Salenium

package newPackage;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;


public class importLeads {

	public static void main(String args[]) throws Exception {
		
		        System.setProperty("webdriver.chrome.driver", "C:\\Users\\David\\Downloads\\Notes\\WebDriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    //login to site
    driver.get("https://demos5.softaculous.com/Mautic/s/contacts/import/new");
    driver.manage().window().maximize();
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.id("password")).sendKeys("password");
    driver.findElement(By.className("btn")).click();
    Thread.sleep(2000);

    //importing Data
    WebElement uploadBox = driver.findElement(By.id("lead_import_file"));
    uploadBox.sendKeys("C:\\Users\\David\\Downloads\\data_file.csv");
    driver.findElement(By.id("lead_import_start")).click();

    //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();

	}
}

Here is the link to the data file ufile.io/vy2ws

In order for the upload of the .CSV file to work, you must be running a local version of this software. Download can be found here: https://www.mautic.org/m/asset/54:mautic-2100

Once uploaded, it should look like this where it asks to choose from the drop-down.

enter image description here

HTML

<div class="col-sm-4">
   <div class="row">
      <div class="form-group col-xs-12 ">
         <label class="control-label" for="lead_field_import_email_address">Email address</label>        
         <div class="choice-wrapper">
            <select id="lead_field_import_email_address" name="lead_field_import[email_address]" class="form-control" autocomplete="false" style="display: none;">
               <option value=""></option>
               <optgroup label="Contact">
                  <option value="email">Email</option>
               </optgroup>
            </select>
            <div class="chosen-container chosen-container-single chosen-with-drop chosen-container-active" style="width: 100%;" title="" id="lead_field_import_email_address_chosen">
               <a class="chosen-single chosen-default">
                  <span>Choose one...</span>
                  <div><b></b></div>
               </a>
               <div class="chosen-drop">
                  <div class="chosen-search"><input type="text" autocomplete="off"></div>
                  <ul class="chosen-results">
                     <li class="group-result">Contact</li>
                     <li class="active-result group-option" data-option-array-index="9" style="">Email</li>
                  </ul>
               </div>
            </div>
         </div>
      </div>
   </div>
</div>

Replacing :

WebElement dropdownToggle = driver.findElement(By.xpath("id('lead_field_import_email_add‌​ress')")); 

to this :

WebElement dropdownToggle = driver.findElement(By.id("lead_field_import_email_address"))‌​;

resulted in:

Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 35535
Only local connections are allowed.
Oct 22, 2017 1:20:49 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"lead_field_import_email_address"}
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z'
System info: host: 'DAVID-PC', ip: '192.235.0.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=XP, acceptSslCerts=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), userDataDir=C:\Users\David\AppData\Local\Temp\scoped_dir4068_28713}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=61.0.3163.100, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}]
Session ID: ee90469095e7b1121dc2e387d8e485e6
*** Element info: {Using=id, value=lead_field_import_email_address}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:402)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
    at newPackage.importLeads.main(importLeads.java:31)

Replacing

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

with

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

resulted in:

Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 32443
Only local connections are allowed.
Oct 23, 2017 1:36:09 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"lead_field_import_email_address"}
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z'
System info: host: 'HOME-PC', ip: '192.235.0.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=XP, acceptSslCerts=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), userDataDir=C:\Users\David\AppData\Local\Temp\1\scoped_dir5416_25737}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=61.0.3163.100, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}]
Session ID: 40cde314a5a76400aceff8b625b38e3c
*** Element info: {Using=id, value=lead_field_import_email_address}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:402)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
    at newAutomation.importLeads.main(importLeads.java:33)

like image 765
JamesRocky Avatar asked Oct 21 '17 04:10

JamesRocky


People also ask

How do I select a dropdown in BootStrap?

To open the dropdown menu, use a button or a link with a class of . dropdown-toggle and the data-toggle="dropdown" attribute. The . caret class creates a caret arrow icon (), which indicates that the button is a dropdown.

How select down from dropdown in Selenium?

Select select = new Select(driver. findElement(By.id("oldSelectMenu"))); // Get all the options of the dropdown List<WebElement> options = select. getOptions(); Using this method, we can retrieve all the options of a dropdown (be it single-select or multi-select ).

What is the return type of getOptions () method in Dropdowns?

We can extract all the options in a dropdown in Selenium with the help of Select class which has the getOptions() method. This retrieves all the options on a Select tag and returns a list of web elements. This method does not accept any arguments.


1 Answers

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.

like image 102
undetected Selenium Avatar answered Oct 02 '22 13:10

undetected Selenium