无法通过硒单击工具提示中的链接

时间:2018-07-04 18:31:59

标签: java selenium selenium-webdriver tooltip

您可以访问makemytrip.com。将鼠标悬停在“行程”上,然后单击“取消预订”。 这是我要执行的代码,不知道我要去哪里错了。

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;
  }

2 个答案:

答案 0 :(得分:1)

您必须使用Actions类执行鼠标悬停操作,然后需要执行以下必需的操作

工作代码:

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

答案 1 :(得分:0)

下面是代码:

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

}

希望它对您有帮助。它正在工作。