Is it possible to pass values using sendkeys in Selenium in order to select an item from a Bootstrap dropdown menu?

Is there a way to pass values for a bootstrap drop down menu using Selenium instead of selecting them? Can we use sendKeys in Selenium to achieve this? I need to input values CSS, HTML, JavaScript without manually selecting them during automation.

<div class="container">
  <h2>Dropdown Example</h2>
  <p>The data-toggle="dropdown" attribute is used to open the dropdown menu.</p>
  <div class="dropdown">
    <button class="btn btn-primary dropdown-toggle" id="menu1" type="button" data-toggle="dropdown">Dropdown Example
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">HTML</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">CSS</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">JavaScript</a></li>
      <li role="presentation" class="divider"></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">About Us</a></li>    
    </ul>
  </div>
</div>

Answer №1

If you want to use your keyboard to select a dropdown list item, you can apply the following code:

new Actions(driver).sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ARROW_DOWN).perform();

For instance, this code will help you choose the 'Javascript' element after clicking on the dropdown button.

update

To determine the position of your desired element in the dropdown, you need to iterate through and count the elements using this method:

List<WebElement> elements = driver.findElements(By.cssSelector(ul.dropdown-menu > li));

for(WebElement element : elements) {
    new Actions(driver).sendKeys(Keys.ARROW_DOWN).perform();
    if(element.getText().equals("Javascript")) {
        element.click();
        break;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to find the XPATH element with Selenium in Python due to element not being located

Can anyone help me troubleshoot? My XPATH seems correct, but it's showing 'no element found' error. I also attempted using find_elements(By.XPATH, "/html/body/div[3]/div[3]/div[5]/div[1]/table[*]/tbody/tr[*]/td[1]/a") import time from selen ...

Python Selenium problem: element state is invalid

Encountering an error message stating "invalid element state" while utilizing the Chrome driver for selenium. The task at hand involves inputting data into The initial problem arises when attempting to utilize the .click() method on the checkbox labeled ...

What causes Selenium ITakeScreenshot to capture a black screen from an IE window?

While working with a Class Library.dll that contains SpecFlow tests written in C# / Selenium, I encountered an issue where using the standard code to capture a screenshot resulted in just a black window. This was puzzling as previous builds and .dll files ...

The attribute "parallel" set to "none" must be one of the following values: false, methods, tests, classes, or instances

I have set up a Maven project using Selenium WebDriver and integrated TestNG. However, when I validate my Maven project, an error message is displayed stating "Attribute "parallel" with value "none" must have a value from the list "false methods tests clas ...

Using Selenium to modify the sorting of Google Maps reviews

I am encountering a fascinating issue with my **Web Scraping** project. My goal is to retrieve the latest **Google Maps reviews**. I specifically need to arrange the reviews based on their date of posting. Although most tutorials I've come across a ...

When Selenium is not in headless mode, it cannot capture a screenshot of the entire website

Disclaimer: Although there is a similar question already out there, none of the answers provided worked for a headless browser. Therefore, I have decided to create a more detailed version addressing this specific issue (the original question I referred to ...

Exploring Firefox extension elements with Selenium: A step-by-step guide

I am looking to extract a link from a notification generated by an extension using Selenium for testing purposes across various websites. The specific extension I am working with is demonstrated in this GitHub example full code available here. It showcase ...

Discovering a specific element within a nested element using Selenium WebDriver

I have encountered an issue while trying to search for an element within a sub-element using Selenium (Version 2.28.0). It seems that Selenium does not restrict its search to the specified sub-element. I am unsure if my approach is incorrect or if there is ...

Having trouble selecting the input text box element in Selenium using Java

My goal is to extract data from an input text box and update it without using id or name. Each input tag will have different values for id, name and value. Below is the HTML code snippet for one of the input tags: <input id="rgrdSearchResult_ctl00_ctl0 ...

The Selenium test functions correctly in the production environment, however it encounters issues when run

I am facing an issue with my Vue.js application while writing Selenium tests. The test passes when run against the deployed production version of the app, but fails when run against a local version. When running the app locally, it is not from a build, bu ...

Encountering a Problem with Github Actions: Exit Code 2 Detected

I need help with the following code snippet for setting up Github Actions: name: Python application on: push: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Install dep ...

Tips for extracting data from a website that requires scrolling down repeatedly

I have a specific website where I always need to scroll down to access the information My code is provided below, however, I am not able to retrieve anything from it # -*- coding: UTF-8 -*- import csv from parsel import Selector from time import sle ...

Could not find an iframe element using the specified xpath

Every time I attempt to run the code below, I encounter an InvalidSelector exception: List<WebElement> allFrames = driver.findElements(By.xpath("//iframe")); org.openqa.selenium.InvalidSelectorException: Unable to find any element using the xpat ...

Tips for conducting Performance Testing using Selenium

I have been trying out new things lately, specifically focusing on selenium for the past 4 months. Can selenium be used for performance testing? If so, how can it be done? I would appreciate any information provided. ...

Selenium Server operates in a manner distinct from WebDriver

I'm experiencing some challenges with Selenium. Let me try to outline the setup I have and what I am attempting to accomplish. Summarized Version: My automated tests run smoothly locally using a Ruby webdriver gem. However, when I run the same scrip ...

Python3 selenium can be utilized to extract the profile image link from a Facebook account

How can I fetch the Facebook profile image link using Python3 and Selenium? Upon inspecting the profile photo element, the following information is obtained: <image style="height: 168px; width: 168px;" x="0" y="0" height="100%" preserveAspectRatio="xM ...

Error: Session ID is empty. Attempting to use WebDriver after quitting. Build information: version '3.0.1'

package com.epath.smoketest.tests; import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.Iterator; import java.util.Properties; import org.apache.commons.io.FileUtil ...

Using CKEditor with Selenium in Python

I am currently in the process of automating an email generator on a work-related site using selenium. However, I have hit a roadblock as I am unable to get selenium to interact with the ckeditor on the page. Despite attempting various methods like switchin ...

The compatibility issue between Selenium WebDriver and Firefox version 7.0.1

Currently, I am working on a project that involves running Selenium tests. After upgrading Firefox to version 7.0.1, the "WebDriver for Firefox" extension used by these tests is no longer functioning properly. Is there a way to resolve this issue without ...

Exploring ways to enhance Ext JS 4 row editing by incorporating custom attributes for better identification in Selenium testing within a grid

Looking for help with assigning static IDs or custom attributes to form fields and buttons in an Ext JS grid using row editing plugin for Selenium test scripts. I know how to rename button text but need to add an attribute for referencing in testing. For ...