我已经在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();
}
}
您是否尝试在没有“ build”的情况下编写它? :-)如果不起作用,您确定xpath是正确的吗?
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//*[@id=\"highlight-addons\"]")));
action.perform();
一切都正确。只需在如下所示的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();
要使用Selenium中ADD-ONS选项卡上的https://www.spicejet.com/来演示Mouse Hover功能,您需要为WebDriverWait引入visibilityOfElementLocated()
,并且可以使用以下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();
}
}
浏览器快照