Selenium 下拉选择在 https://www.goibibo.com/hotels/ 网站

Selenium Drop Down Selection In https://www.goibibo.com/hotels/ website

https://www.goibibo.com/hotels/ Image

我想 select 一个城市使用 selenium,但它不起作用 我试过 xpath,css select 或者,id,className.\

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Goibibo {
    
    public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "C:\Selenium\chromedriver_2\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
     //Goto Url Goibibo.com
     driver.get("https://www.goibibo.com/hotels/");
     System.out.println("WWW.GOIBIBO.COM");
     
     //Select Country India
     driver.findElement(By.xpath("//input[@name='CountryType']")).click();

     System.out.println("Selected Country India");
     // Click on Search Bar
     driver.findElement(By.id("downshift-1-input")).click();

     System.out.println("Clicked On Search Bar");

     //Select Mysore City
     driver.findElement(By.xpath("//*[@id=\"downshift-1-menu\"]/div/ul/li[2]/img")).click();;
     }
}

您可以尝试使用以下 xpath :

//p[text()='Mysore']

//img[contains(@alt,'Mysore')]/following-sibling::div

代码中 :

System.out.println("Clicked On Search Bar");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p[text()='Mysore']"))).click();
System.out.println("Task has been done !");