[英]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.