Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将鼠标悬停在菜单上,然后选择JavaSelenium中的子菜单_Java_Selenium_Selenium Chromedriver - Fatal编程技术网

将鼠标悬停在菜单上,然后选择JavaSelenium中的子菜单

将鼠标悬停在菜单上,然后选择JavaSelenium中的子菜单,java,selenium,selenium-chromedriver,Java,Selenium,Selenium Chromedriver,我尝试将鼠标悬停在主菜单上,并使用java selenium选择子菜单,我让它悬停在菜单上,但无法选择子菜单,如果我尝试通过linktext查找,如果我使用xpath表示构建成功但未打开新页面,则始终会出现错误“不存在”。这是我到目前为止的代码 System.setProperty("webdriver.chrome.driver","C:/Driver/chromedriver.exe"); WebDriver webDriver = new ChromeDriver();

我尝试将鼠标悬停在主菜单上,并使用java selenium选择子菜单,我让它悬停在菜单上,但无法选择子菜单,如果我尝试通过linktext查找,如果我使用xpath表示构建成功但未打开新页面,则始终会出现错误“不存在”。这是我到目前为止的代码

System.setProperty("webdriver.chrome.driver","C:/Driver/chromedriver.exe");      
    WebDriver webDriver = new ChromeDriver();       
    webDriver.manage().window().maximize();
    webDriver.navigate().to("https://www.skiutah.com");

    String NavTo = "DEALS";
    String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
    WebElement element = webDriver.findElement(By.linkText(NavTo));
    WebElement el = webDriver.findElement(By.xpath(pathx));
    Actions action = new Actions(webDriver);
    action.moveToElement(element).perform();
    action.moveToElement(el).click();

我认为您在子菜单上悬停和单击的方式似乎不正确

您还没有共享您的
html
,所以检查您找到的元素是否正确有点繁琐。如果一切正常,请尝试以下代码,这可能会对您有所帮助-

WebElement menu =  driver.findElement(By.your_locator);
WebElement sub_menu =  driver.findElement(By.your_locator);
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(sub_menu).click().build().perform();
解释:-


这里使用
build()
方法将所有操作列表编译为一个步骤,并准备好执行在WebDriver中,我们提供了控制
鼠标事件的选项。试试这段代码。这应该达到目的

driver.get("https://www.skiutah.com/");
WebElement deals = driver.findElement(By.xpath("//a[@title='Deals']"));
Mouse mouse = ((HasInputDevices) driver).getMouse();
Locatable hoverItem = (Locatable) deals;
mouse.mouseMove(hoverItem.getCoordinates());
WebElement beginner = driver.findElement(By.xpath("//a[text()='Beginner']"));
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOf(beginner));
Locatable clickItem = (Locatable) beginner;
mouse.mouseDown(clickItem.getCoordinates());
mouse.mouseUp(clickItem.getCoordinates());
System.out.println(driver.getTitle());

要使用鼠标悬停操作,我们需要使用build.perform。它被称为动作链,确保它在最后一起执行动作。或者,您可以按如下方式交换线路,它应该适合您。我试过了,看起来不错

String NavTo = "DEALS";
String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
WebElement element = webDriver.findElement(By.linkText(NavTo));
WebElement el = webDriver.findElement(By.xpath(pathx));
Actions action = new Actions(webDriver);
action.moveToElement(el).click();
String NavTo = "DEALS";
String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
WebElement element = webDriver.findElement(By.linkText(NavTo));
WebElement el = webDriver.findElement(By.xpath(pathx));
Actions action = new Actions(webDriver);
action.moveToElement(el).click();
action.moveToElement(element).perform();

首先将鼠标悬停在主菜单上,然后单击任意子菜单

WebDriverWait Wait = new WebDriverWait(driver,10);
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//[@id='top_menu']/ul/li[4]/a"))));
Actions mousehover = new Actions(driver);
mousehover.moveToElement(driver.findElement(By.linkText("Deals"))).build().perform();
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText("All Deals"))));
driver.findElement(By.linkText("All Deals")).click();

我正在尝试“单击”子菜单。需要将鼠标移到主菜单上,几秒钟后加载子菜单。然后我需要找到子菜单并单击它。 这是我使用的代码

 Actions ac = new Actions(dr);  
 WebElement we = dr.findElement(By.xpath(".//*[@id='ddtopmenubar']/ul/li[1]/a"));
 ac.moveToElement(we).build().perform();
 WebDriverWait wait = new WebDriverWait(dr, 5); 
 wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a")));
 WebElement e= dr.findElement(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a"));
 e.click();
但这似乎不起作用

将异常获取为: org.openqa.selenium.WebDriverException:性能 生成信息:版本:“未知”,版本:“未知”,时间:“未知”


当我在调试模式下执行相同操作时,我可以单击子菜单。

甚至我尝试了以下代码:WebElement menu=driver.findElement(By.your_locator);WebElement子菜单=driver.findElement(通过.your\u定位器);动作动作=新动作(驱动);action.moveToElement(菜单)。moveToElement(子菜单)。单击().build().perform();
WebDriverWait Wait = new WebDriverWait(driver,10);
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//[@id='top_menu']/ul/li[4]/a"))));
Actions mousehover = new Actions(driver);
mousehover.moveToElement(driver.findElement(By.linkText("Deals"))).build().perform();
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText("All Deals"))));
driver.findElement(By.linkText("All Deals")).click();
 Actions ac = new Actions(dr);  
 WebElement we = dr.findElement(By.xpath(".//*[@id='ddtopmenubar']/ul/li[1]/a"));
 ac.moveToElement(we).build().perform();
 WebDriverWait wait = new WebDriverWait(dr, 5); 
 wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a")));
 WebElement e= dr.findElement(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a"));
 e.click();