簡體   English   中英

How to select the autocomplete result that contains a certain phrase within the website https://www.easyjet.com/en using Selenium and Java

[英]How to select the autocomplete result that contains a certain phrase within the website https://www.easyjet.com/en using Selenium and Java

我正在針對以下網站練習https://www.easyjet.com/en

我將“London”的值傳遞到 Origin 搜索框中。 這將返回六個機場匹配項。 然后我試圖搜索結果和 select 包含單詞“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);
        }
    }
}

這只是不斷循環,找不到匹配項。 我嘗試了各種短語,但沒有快樂。

任何人都可以幫忙嗎?

在將London的值傳遞到Origin搜索框時,要單擊文本為Luton的自動完成/自動建議,您必須為visibilityOfAllElementsLocatedBy()誘導WebDriverWait ,然后使用以下定位器策略在所需元素上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(); } } }
  • 瀏覽器快照:

盧頓

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM