Reputation: 161
Selenium with Java: I try to click, scroll and choose Varadero as my destination.
I have this:
driver.findElement(By.id("TOSEARCH")).click();
driver.findElement(By.id("TOSEARCH")).sendKeys("Varadero");
driver.findElement(By.xpath("//span[contains(@class,'name')]//em[contains(text(),'Varadero')]")).click();
Upvotes: 0
Views: 42
Reputation: 3056
You should first click to choose the from
(De
in French) in order to be able to click and select the destination to
(Vers
in French). And each source and destination have their unique Id that can be used to identify them easily.
here's the how you may approach:
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;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.time.Duration;
public class TransatSearchAutomation {
public static void main(String[] args) {
// Setup ChromeDriver using WebDriverManager
WebDriverManager.chromedriver().setup();
// Set ChromeOptions
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--disable-notifications");
options.addArguments("--disable-popup-blocking");
options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
options.setExperimentalOption("useAutomationExtension", false);
// Initialize WebDriver
WebDriver driver = new ChromeDriver(options);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
try {
// Open the website
driver.get("https://www.transat.com/fr-CA?search=package");
// Wait for 'FROMSEARCH' element and click
WebElement fromSearch = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("FROMSEARCH")));
fromSearch.click();
Thread.sleep(2000);
// Select departure city
WebElement departureCity = driver.findElement(By.cssSelector("#YUL-FROMSEARCH > span.code"));
departureCity.click();
// Wait for 'TOSEARCH' element and click
WebElement toSearch = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("TOSEARCH")));
toSearch.click();
Thread.sleep(2000);
// Select destination city
WebElement destinationCity = driver.findElement(By.cssSelector("#City-13-TOSEARCH > div > span.name"));
destinationCity.click();
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
Upvotes: 0