java - expedia“从”机场列表中的自动下拉列表
问题描述
我需要您的帮助,我正在尝试在 www.Expedia.com 的“从”下选择机场名称的自动下拉列表。我的代码成功运行但没有生成所需的输出。
使用这行代码,我将密钥发送到“flying from”
driver.findElement(By.xpath("//input[@placeholder='City or airport']")).sendKeys("London");
然后使用此代码,我试图从自动下拉列表中捕获希思罗机场,但我的代码不是英国伦敦(LHR-希思罗机场),而是选择英国伦敦(STN-斯坦斯特德)。
List<WebElement> list = driver.findElements((By.xpath("//div[@class='autocomplete-dropdown']")));
for (int i=0;i<list.size();i++){
System.out.println(list.get(i).getText());
if(list.get(i).getText().contains("Heathrow")){
list.get(i).click();
break;
}
}
当前产出:英国伦敦(SEN-绍森德) 预期产出:英国伦敦(LHR-希思罗)
下面是我试图点击自动建议的代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class expedia_search {
public static void main (String args[]) {
// Set the property for webdriver.chrome.driver to be the location to your local download.
System.setProperty("webdriver.chrome.driver", "/Users/vc/IdeaProjects/expedia_search/src/chromedriver");
// Create new instance of ChromeDriver
WebDriver driver = new ChromeDriver();
// And now use this to visit expedia.com
driver.get("http://www.expedia.com");
// Find the text input element by its absolute path(xpath)
WebElement element = driver.findElement(By.xpath("//*[@id=\"tab-flight-tab-hp\"]"));
// Once flight tab selected click on it
element.click();
//type london on Expedia from tab
driver.findElement(By.xpath("//input[@placeholder='City or airport']")).sendKeys("London");
//capture auto suggestions from expedia from
List<WebElement> list = driver.findElements((By.xpath("//div[@class='autocomplete-dropdown']")));
for (int i=0;i<list.size();i++){
System.out.println(list.get(i).getText());
if(list.get(i).getText().contains("Heathrow")){
list.get(i).click();
break;
}
}
}
}
解决方案
欢迎来到 SO。这是可用于直接选择机场而不使用循环的 xpath 和 css。
CSS:
.autocomplete-dropdown a[data-value*='LTN']
xpath: //div[@class='autocomplete-dropdown']//a[contains(@data-value,'Luton')]
硒实现:
driver.findElement(By.xpath("//div[@class='autocomplete-dropdown']//a[contains(@data-value,'Luton')]")).click();
或者
driver.findElement(By.cssSelector(".autocomplete-dropdown a[data-value*='Luton']")).click();
推荐阅读
- sql - 从一个表流中获取所有行,并从另一个表中及时获取之前的行
- python - 搜索数据框列中是否存在“,”并将其拆分为 2 个数据框列
- firebase - Firestore 请求域特定的安全规则?
- python - 尝试从 environment.yml 文件创建环境时出现“ResolvePackageNotFound”错误
- excel - 过滤两个数组以确保如果类别为 x 它是唯一的
- python - 获取在 Linux 上连接的新 USB 键盘的事件
- java - 在 Eclipse 中将 .jsp 扩展名更改为 .xyz
- typescript - 覆盖类型定义文件中声明的 var 类型
- python - 将多个文件夹的名称存储在变量列表中
- google-apps-script - 将最后更新的行从 Sheet1 复制到 Sheet2