【问题标题】:MouseHover Functionality in SeleniumSelenium 中的 MouseHover 功能
【发布时间】:2020-05-20 10:16:46
【问题描述】:

我在https://www.spicejet.com/ 内的ADD-ONS 选项卡上创建并执行了MouseHover 功能,但它不适用于以下代码。

谁能建议代码中缺少什么?

package SeleniumExamples;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class MouseHoverConcept {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","./driver/chromedriver81.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
        driver.get("https://www.spicejet.com/");
        // create object for mouse hover
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.xpath("//*[@id=\"highlight-addons\"]"))).build().perform();
        Thread.sleep(3000);
        driver.findElement(By.linkText("SpiceMax")).click();
        //driver.quit();
    }
}

【问题讨论】:

    标签: java selenium selenium-webdriver webdriverwait mousehover


    【解决方案1】:

    要在https://www.spicejet.com/ 内的ADD-ONS 选项卡上使用Selenium 演示鼠标悬停 功能,您需要为visibilityOfElementLocated() 引入WebDriverWait 和您可以使用以下Locator Strategy

    • 代码块:

      import java.util.Collections;
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.chrome.ChromeDriver;
      import org.openqa.selenium.chrome.ChromeOptions;
      import org.openqa.selenium.interactions.Actions;
      import org.openqa.selenium.support.ui.ExpectedConditions;
      import org.openqa.selenium.support.ui.WebDriverWait;
      
      public class mouseHover_spicejet_addons {
      
          public static void main(String[] args) {
      
              System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
              ChromeOptions options = new ChromeOptions();
              options.addArguments("--start-maximized");
              options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
              options.setExperimentalOption("useAutomationExtension", false);
              WebDriver driver =  new ChromeDriver(options);
              driver.get("https://www.spicejet.com/");
              WebElement addons = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a#highlight-addons")));
              new Actions(driver).moveToElement(addons).build().perform();
              new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='add-ons-tab']//li/a[contains(., 'SpiceMax')]"))).click();
          }
      }
      
    • 浏览器快照

    【讨论】:

      【解决方案2】:

      您是否尝试在没有“构建”的情况下编写它? :-) 如果它不起作用,你确定 xpath 是正确的吗?

      Actions action = new Actions(driver);
      action.moveToElement(driver.findElement(By.xpath("//*[@id=\"highlight-addons\"]")));
      action.perform();
      

      【讨论】:

        【解决方案3】:

        一切都是正确的。只需在 get 方法之后添加 10 秒的睡眠,如下所示

             driver.get("https://www.spicejet.com/");
             driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
             Thread.sleep(10000);
        
        
             // create object for mouse hover
                Actions action = new Actions(driver);
                action.moveToElement(driver.findElement(By.xpath("//a[@id='highlight-addons']"))).build().perform();
                Thread.sleep(3000);
                driver.findElement(By.linkText("SpiceMax")).click();
                //driver.quit();
        

        【讨论】:

          【解决方案4】:

          mouseHover 在 Chrome 中不一致。你可以试试 JavaScript Executor。下面是Java中的sn-p代码:

          WebElement elementToHover = driver.findElement(By.xpath("//*[@id='highlight-addons']"));
          String mouseHoverScript = "if(document.createEvent)" +
                          "{var evObj = document.createEvent('MouseEvents');" +
                          "evObj.initEvent('mouseover', true, false); " +
                          "arguments[0].dispatchEvent(evObj);} " +
                          "else if(document.createEventObject) " +
                          "{ arguments[0].fireEvent('onmouseover');}";
          ((JavascriptExecutor) driver).executeScript(mouseHoverScript, elementToHover);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-08-20
            • 1970-01-01
            • 2010-09-20
            • 2011-10-03
            • 2019-01-06
            • 2012-05-23
            • 1970-01-01
            • 2011-09-29
            相关资源
            最近更新 更多