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
1 Answer
Reset to default 0You 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条)