Selenium, Java: Click, scroll to find the city (Varadero) - Stack Overflow

Selenium with Java: I try to click, scroll and choose Varadero as my destination.I have this:driver.fi

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

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();
Share Improve this question asked yesterday Tanguy MPTanguy MP 1599 bronze badges 2
  • this list item with the ID probably has the handler, so target that. .click() method will automatically scroll into view. Ex: driver.findElement(By.id("City-13-TOSEARCH")).click(); – browsermator Commented yesterday
  • 1 @browsermator Works perfectly, thanks – Tanguy MP Commented 19 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

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 .openqa.selenium.By;
import .openqa.selenium.WebDriver;
import .openqa.selenium.WebElement;
import .openqa.selenium.chrome.ChromeDriver;
import .openqa.selenium.chrome.ChromeOptions;
import .openqa.selenium.support.ui.ExpectedConditions;
import .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/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();
        }
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1740048163a4187142.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信