Choosing a dropdown option with a CSS Selector

Here is the code snippet I am currently working with:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class VirginTrains {

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

String projectPath = System.getProperty("user.dir");
System.out.println("projectPath : " + projectPath);

// THE FOLLOWING CODE IS FOR 1.Journey details
//Opens virgin trains web page
System.setProperty("webdriver.chrome.driver", projectPath + "\\drivers\\chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.virgintrains.co.uk/");

//The following maximise the screen size
driver.manage().window().maximize();

The code snippet below attempts to select the "Arriving by" option from a drop down:

// click to open the date time picker calendar - 4th Box
Actions actions1 = new Actions(driver);
WebElement departArrive = driver.findElement(By.cssSelector("span[class='dropdownLabel']"));
actions1.doubleClick(departArrive).perform();

driver.findElement(By.cssSelector("span[class='dropdownLabel']")).click();
driver.findElement(By.cssSelector("css=input.Arriving by")).click();

Upon execution, the code should successfully select the "Arriving by" option.

Answer №1

I understand that you are looking for:

[data-value="Arrive Before"]

In the future, consider using your browser's elements panel to inspect and select attributes for more efficient navigation.

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

Is there a way to tally rows across various pages of a table using robot framework?

I've recently delved into the world of robotics and have been working on it independently at my workplace for about a month. Currently, I am faced with the challenge of counting the total number of rows in a table within the application I am testing ...

Experiencing problems with CSS compatibility while transitioning from vanilla JavaScript to React

Currently, I am working on revamping the frontend of an app that was initially built with vanilla javascript and transitioning it to a React framework. However, I'm encountering some challenges with styling the HTML elements. Specifically, the textare ...

The datatable does not adjust to changes in page size

My jsfiddle example showcases different card boxes, and I am currently focusing on making the 'Table 1' box responsive. However, I have encountered an issue: Check out my jsfiddle example here code If you open the jsfiddle with a large enough ...

"Entering a text message will prompt the appearance of the on-screen keyboard in capital

Our website is optimized for use on Android tablets. I have added the following CSS to the input's class: text-transform: uppercase; While this successfully displays typed letters in uppercase, it does not change the appearance of the on-screen keyb ...

I'm wondering how I can pass an argument in the command line interface and then use it in my Python code. For example, if I were to write "pytest --headless"

How can I modify conftest.py to enable headless mode when running test.py with the command pytest --headless in the terminal? By default, it should run in regular mode (show browser). conftest.py: def pytest_addoption(parser): parser.addoption("- ...

Experiencing difficulties when attempting to deploy script to AWS Lambda

My current challenge involves executing a script that utilizes Selenium and specifically webdriver. driver = webdriver.Firefox(executable_path='numpy-test/geckodriver', options=options, service_log_path ='/dev/null') The problem I am ...

Selenium encounters Timeout::Error when trying to load a resource-intensive webpage

Encountering a Timeout::Error problem while loading a large web page. Upon investigation, it seems that even with an implicit wait call @driver.manage.timeouts.implicit_wait = 300, the error occurs before the set 300 seconds - typically around 60-70 second ...

Utilizing Bootstrap to position a navigation bar next to a floated element causes surrounding content to be pushed

I am facing an issue with my 2 column layout where the content in the right column is being pushed down to clear the left column, resulting in a lot of vertical white space between the navigation and the content. Below is the code snippet: <head> ...

Multiple columns contained within a span

I'm trying to create a panel that displays a list of values with corresponding labels. The requirement is for each label to be positioned above its respective value and aligned to the left. The layout should resemble the following: Label 1 Lab ...

Modify the text of a button when hovered over (JavaFX)

Can anyone help me figure out how to change the text of a button when it is hovered over? Approach: if (button.isHover()){ button.setText("new message"); } I'm open to either a css fix or a code solution! ...

Encountering a 'connection refused' error while trying to execute Selenium with a Headless Firefox browser

My current setup includes Selenium 3.4.0, GeckoDriver v0.19.0 for Linux 64-bit, and Firefox 52.4.0. This is my first time posting a question on this platform. Normally, I can find solutions by browsing through discussions, but this particular issue has be ...

When you hover, there is no writing cursor in sight

I'm looking for a way to display a label on an input field and have a writing cursor show up when hovering over the label. I am not interested in using a placeholder label. Do you have any simple solutions for this? See the code snippet below: JSFiddl ...

Layering one canvas on top of another

Here is the code I am working with. With a JavaScript library, I can draw on the first canvas and then merge it onto the second canvas. My question is, how can I make the first canvas stay in place or float, regardless of where I scroll on the second canv ...

Creating a button in ReactJS with text displayed under an icon

Presently, I am working with a component that looks like this: https://i.stack.imgur.com/qGCwj.png This is the code for the component: import React from "react"; import {withStyles} from "material-ui/styles"; import Settings from "material-ui-icons/Setti ...

Is there a way to utilize expected_conditions.text_to_be_present_in_element_value, but with the _text parameter set to any String taken from a tuple?

In my current code, I have set up a waiting mechanism for one minute or until the _text parameter of EC.text_to_be_present_in_element_value matches the String value to_inp: # Python to_inp = "secret" WebDriverWait(driver, 60).until(EC.text_to_be_ ...

The concept of Theme.breakpoints does not exist in MUI React, there is

I keep running into the same error where theme.breakpoints is undefined when I try to use theme.breakpoints.up in my code. The versions of the dependencies I am currently using are: "@emotion/react": "^11.9.0", "@emotion/styled&quo ...

Grid-based timekeeping design

Currently working on a clock grid layout and seeking some advice on the next steps. The layout has been created but still needs some adjustments to achieve perfection. What would be the most effective approach for correcting it? Would offsetting it using a ...

Toggle between different icons with Bootstrap 4

Can someone help me with changing the bootstrap 4 navbar-toggler-icon appearance when it's open or closed using CSS? I have been trying to figure it out but so far, I haven't found a straightforward solution. .map-controls-mobile .navbar-toggler ...

What is the best way to create a sidebar that remains open without triggering a new sidebar every time it is clicked

I am looking to add a sidebar that opens a new view without navigating to a different page. The sidebar remains consistent and I want to avoid duplicating it on all pages. Check out this photo for an example. My goal is to provide additional content or fe ...

Using Selenium in Python, identify and choose a class

Hello, I am currently working on automating a process using selenium, python, and GLPI. I have been struggling with selecting a user from a menu bar, despite trying various methods like linkText, cssSelector, and xpath. I suspect I may be doing something w ...