如何使用Selenium和Java在网站内选择包含某个短语的自动完成结果https:/www.easyjet.comen。

问题描述 投票:0回答:1

我正在对以下网站进行练习 https:/www.easyjet.comen

我将 "伦敦 "的值传入原产地搜索框。这将返回六个匹配的机场。然后我试图从结果中选择包含 "Luton "一词的机场。

到目前为止,我的代码是。

package d_practise;

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 easyjetMenu {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "C:\\Work\\Drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.easyjet.com/");

        Thread.sleep(2000);
        WebElement d = driver.findElement(By.cssSelector("input[name='origin']"));
        d.click();
        d.sendKeys("London");

        while(!d.getText().contains("Luton")) {
            d.sendKeys(Keys.DOWN);
        }
        if(d.getText().contains("Luton")) {
            d.sendKeys(Keys.ENTER);
        }
    }
}

这只是不断地循环,没有找到匹配的词。我已经尝试了各种短语,但没有成功。

有谁能帮助我?

java selenium autocomplete css-selectors webdriverwait
1个回答
1
投票

在传递值 伦敦 进入 起源 搜索框中,点击自动完成自动建议,文字为 卢顿 你要诱导 WebDriverWait 对于 visibilityOfAllElementsLocatedBy() 然后 click() 在所需元素上使用以下方法 定位器策略:

  • 代码块。

    import java.util.Collections;
    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class easyjet_com_origin {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://www.easyjet.com/en/");
            WebElement d = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='origin']")));
            d.click();
            d.sendKeys("London");
            //List<WebElement> origins = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@id='ui-id-1']//li/a/span")));
            List<WebElement> origins = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("ul#ui-id-1 li>a>span")));
            for(WebElement origin:origins)
            {
                if(origin.getText().contains("Luton"))
                    origin.click();
            }
        }
    }
    
  • 浏览器快照。

Luton

© www.soinside.com 2019 - 2024. All rights reserved.