Reputation: 37
You can visit on makemytrip.com. Hover on "trips" and click on "cancel bookings". Here is the code what I am trying to execute and don't know where am I going wrong.
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
".\\exeFile\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.navigate().to("https://www.makemytrip.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
List<WebElement> dd_values=driver.findElements(By.xpath("//li[@class='menu-
trigger']//ul[@class='ch__profileOverlayTabs ch__capitalize
append_bottom20']//li"));
for (WebElement elements: dd_values) {
System.out.println("values of each attribute :
"+elements.getAttribute("innerHTML"));
if (elements.getAttribute("innerHTML").contains("Cancel Bookings")) {
elements.click();
break;
}
Upvotes: 0
Views: 1129
Reputation: 2334
You have to perform the mouse hover action using Actions
class and then need to perform the required action as below
Working Code:
driver.get("https://www.makemytrip.com/");
driver.manage().window().maximize();
//Explicit wait is added after the Page load
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.titleContains("Make"));
WebElement element=driver.findElement(By.xpath("//div[@class='ch__userInteraction ch__clearfix']//span[text()='trips']"));
Actions builder=new Actions(driver);
builder.moveToElement(element).build().perform();
driver.findElement(By.xpath("//div[@class='my_trips log-in-trip ch_trip_logged header-dropdown']//a[text()='Cancel Bookings']")).click();
Upvotes: 1
Reputation: 1275
Below is the code :
package com.demo.core;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.HasInputDevices;
import org.openqa.selenium.interactions.Mouse;
import org.openqa.selenium.interactions.internal.Locatable;
public class MakeMyTripDemo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "J:\\STADIUM\\selenium-demo\\src\\main\\resources\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.navigate().to("https://www.makemytrip.com/");
WebElement trips = driver.findElement(By.xpath("//a[@mt-class='trips_icon']"));
mouseOverElement(driver, trips);
WebElement cancelBooking = driver.findElement(By.xpath("//a[@id='ch_trips_cancel']"));
jsPress(driver, cancelBooking);
}
private static void mouseOverElement(WebDriver driver, WebElement webElement) {
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(((Locatable) webElement).getCoordinates());
}
public static void jsPress(WebDriver driver, WebElement element) {
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
}
}
Hope it helps you. It is working.
Upvotes: 0