我需要你的帮助,我正在尝试在www.Expedia.com的“飞行”下选择机场名称的自动下拉列表。我的代码成功运行但没有生成所需的输出。
使用这行代码我发送密钥“飞行”
driver.findElement(By.xpath("//input[@placeholder='City or airport']")).sendKeys("London");
然后使用这个代码我试图从autodropdown列表捕获希思罗机场,而不是伦敦,英国(LHR-希思罗机场)我的代码选择伦敦,英国(STN-Stansted)。
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-Southend)预期产量:伦敦,英国,英国(LHR-Heathrow)
下面是我的代码我试图点击自动提示
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;
}
}
}
}
那是因为您用来从下拉列表中选择值的xpath是不正确的。你正在使用的xpath是整个下拉列表,所以selenium点击它的中间,点击“STN-Stansted”。您需要将xpath用于下拉列表中显示的列表。 请使用我提到的xpath:
List<WebElement> list = driver.findElements(By.xpath("//li[@class='results-item']"));
欢迎来到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();