admin管理员组文章数量:1026989
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();
}
}
}
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();
}
}
}
本文标签: seleniumJava Clickscroll to find the city (Varadero)Stack Overflow
版权声明:本文标题:Selenium, Java: Click, scroll to find the city (Varadero) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1740048162a1709684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论