简体   繁体   中英

Xpath is not working on MouseOver(CatMenu) in Selenium

There is problem about selenium for all web driver which cannot open mouseover menu.Altough assing to Xpath of element web driver cannot open Mouse Over (CatMenu) and log is that "Must provide a location for a move action".

i want to go n11.com web adress and over on Kitap, Müzik, Film, Oyun and click Kitap but its not working. Thank you

@Test

public void  startWebDriver(){
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.n11.com");


Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath(".//*[@id='contentMain']/div/nav/ul/li[8]/a"))).perform();

}

Use following code to achieve the same -

    System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
    driver =new ChromeDriver();     
    driver.get("http://www.n11.com/");
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    WebElement menu =  driver.findElement(By.xpath("//li[@class='catMenuItem']/a[@title='Kitap, Müzik, Film, Oyun']"));
    WebElement submenu = driver.findElement(By.xpath("//li[@class='subCatMenuItem']/a[@title='Kitap']"));

    Actions action = new Actions(driver);

    action.moveToElement(menu).moveToElement(submenu).click().build().perform();

Use some Implicit Wait to avoid timeout exceptions to find your web element
Use more specific xpath to find your web element.

In your case first you need to hover on Kitap, Müzik, Film, Oyun menu and then have to perform click on Kitap submenu

You can try to use ExplicitWait with more specific XPath :

WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//a[@title='Kitap, Müzik, Film, Oyun']")[2])));
// WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Kitap, Müzik, Film, Oyun")));
Actions act = new Actions(driver);
act.moveToElement(element).perform();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM