我正在尝试处理此网站中的“自动提示”下拉菜单 https://www.makemytrip.com/通过在Eclipse中使用Selenium Webdriver
在Selenium Webdriver中运行此测试用例时,我没有遇到任何异常,但是通过这次测试执行,我没有从该网站上看到预期的结果。
package udemyClasses;
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;
public class AutoSuggestiveDropdowns {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
Thread.sleep(3000);
WebElement f=driver.findElement(By.id("fromCity"));
f.sendKeys("che");
Thread.sleep(2000);
f.sendKeys(Keys.ARROW_DOWN);
f.sendKeys(Keys.ENTER);
Thread.sleep(2000);
f.sendKeys(Keys.TAB);
Thread.sleep(2000);
WebElement t=driver.findElement(By.id("toCity"));
t.sendKeys("del");
Thread.sleep(2000);
t.sendKeys(Keys.ARROW_DOWN);
t.sendKeys(Keys.ENTER);
Thread.sleep(2000);
}
}
实际上,我希望在该页面中将“从城市”选择为“钦奈”,将“到城市”选择为德里。但是他们没有在“从城市”和“到城市”下拉列表中选择任何城市。
答案 0 :(得分:0)
欢迎来到SO。 您可以使用xpath选择城市,如下所示。
driver.get("https://www.makemytrip.com/");
Thread.sleep(3000);
WebElement f=driver.findElement(By.id("fromCity"));
f.sendKeys("che");
Thread.sleep(2000);
f.findElement(By.xpath("//div[contains(@class, 'hsw_autocomplePopup')]//p[contains(.,'Chennai') and contains(@class,'font16')]")).click();
WebElement t=driver.findElement(By.id("toCity"));
t.sendKeys("del");
Thread.sleep(2000);
f.findElement(By.xpath("//div[contains(@class, 'hsw_autocomplePopup')]//p[contains(.,'Delhi') and contains(@class,'font16')]")).click();
Thread.sleep(2000);