使用CSS选择器选择一个下拉值

时间:2019-04-12 21:10:07

标签: css selenium automation

这是我现有的代码:

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

在下面的代码中,我想在下拉菜单中选择“到达依据”

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

代码运行后,应选择到达依据

1 个答案:

答案 0 :(得分:0)

我想看到它,你想要

[data-value="Arrive Before"]

下次,只需在“元素”面板中检查并选择一个属性即可选择。