简体   繁体   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

I am practicing against the following website https://www.easyjet.com/en我正在针对以下网站练习https://www.easyjet.com/en

I am passing a value of "London" into the Origin search box.我将“London”的值传递到 Origin 搜索框中。 This returns six airport matches.这将返回六个机场匹配项。 I'm then trying to trawl through the results and select the one that contains the word "Luton".然后我试图搜索结果和 select 包含单词“Luton”的结果。

My code so far is:到目前为止,我的代码是:

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);
        }
    }
}

This just continuously loops and no match is found.这只是不断循环,找不到匹配项。 I have tried various phrases but no joy.我尝试了各种短语,但没有快乐。

Anyone please able to help?任何人都可以帮忙吗?

On passing the value of London into the Origin search box, to click on the autocomplete/autosuggestion with text as Luton you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and then click() on the desired element using the following Locator Strategy :在将London的值传递到Origin搜索框时,要单击文本为Luton的自动完成/自动建议,您必须为visibilityOfAllElementsLocatedBy()诱导WebDriverWait ,然后使用以下定位器策略在所需元素上click()

  • Code Block:代码块:

     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(); } } }
  • Browser Snapshot:浏览器快照:

卢顿

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 https://www.goibibo.com/hotels 上点击 SELECT ROOM 按钮 - How to click on SELECT ROOM button on https://www.goibibo.com/hotels using Selenium and Java How to extract the text 73 from the text 1-16 of 73 results from the search result summary within https://www.amazon.com using Selenium and Java - How to extract the text 73 from the text 1-16 of 73 results from the search result summary within https://www.amazon.com using Selenium and Java 如何使用 Selenium Java 处理 https://www.spicejet.com/ 的 PASSENGERS 字段的静态下拉列表 - How to handle the static dropdowns of PASSENGERS field of https://www.spicejet.com/ using Selenium Java 如何在网站https://www.phptravels.net上通过Selenium和Java调用显式等待以文本为我的帐户的元素 - How to invoke Explicit wait to click on the element with text as My Account through Selenium and Java within the website https://www.phptravels.net 如何使用 Selenium 和 Java 在 https://www.phptravels.net/ 网站中提取 HTML5 约束验证的文本? - How can I extract the text of HTML5 Constraint validation in https://www.phptravels.net/ website using Selenium and Java? 如何在Java中使用Selenium的下拉列表框中select选项首选国家/地区网站? - How to select the option preferred country/region website from the dropdown listbox within using Selenium in Java? driver.findelements(By.xpath) 在 https://www.amazon.com/ 上使用 Selenium Java 显示不一致的搜索结果 - driver.findelements(By.xpath) shows inconsistent search results on https://www.amazon.com/ using Selenium Java org.xeustechnologies.googleapi.spelling.SpellCheckException:java.io.FileNotFoundException:https://www.google.com/tbproxy/spell?lang=zh_CN&amp;hl=zh-CN - org.xeustechnologies.googleapi.spelling.SpellCheckException: java.io.FileNotFoundException: https://www.google.com/tbproxy/spell?lang=en&hl=en 如何在 Origin.com 中使用 selenium Z93F725A07423321C889DB44 select 登录按钮 - How to select the Sign In button in Origin.com using selenium java 使用Selenium WebDriver无法在www.mail.com/int/中单击“撰写”按钮 - Unable to clicking Compose button within www.mail.com/int/ using Selenium WebDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM