如何 select 使用 Selenium 和 Java 在网站 https://www.easyjet.com/en 中包含特定短语的自动完成结果
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 搜索框中。这returns六场机场比赛。然后我试图浏览结果和 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 搜索框后,单击带有文本 autocomplete/autosuggestion 的 autocomplete/autosuggestion =26=]Luton 您必须为 visibilityOfAllElementsLocatedBy()
引入 WebDriverWait,然后使用以下 [=13= 在所需元素上引入 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();
}
}
}
浏览器快照:
我正在对以下网站进行练习https://www.easyjet.com/en
我将 "London" 的值传递到 Origin 搜索框中。这returns六场机场比赛。然后我试图浏览结果和 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 搜索框后,单击带有文本 autocomplete/autosuggestion 的 autocomplete/autosuggestion =26=]Luton 您必须为 visibilityOfAllElementsLocatedBy()
引入 WebDriverWait,然后使用以下 [=13= 在所需元素上引入 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(); } } }
浏览器快照: