gpt4 book ai didi

java - Selenium WebDriver 将鼠标悬停在菜单上后单击下拉菜单

转载 作者:行者123 更新时间:2023-12-02 12:32:26 25 4
gpt4 key购买 nike

由于将鼠标悬停在主菜单选项“事件”上,我无法单击“新事件”下拉菜单。鼠标悬停导致的菜单问题在于,在出现下拉菜单之前,子菜单元素(所有事件和新事件)不可用于选择。当我运行脚本时,无法找到该元素的存在。

下面是我的 HTML 片段

<div id="navigation">
<nav id="top-nav">
<ul id="left-nav" class="left-navbar">
<a class="no-hover" href="mainMenu.html">
<li class="border-right">
<a id="EventsMenu" href="eventsList.html">Events ▼</a>
<ul class="submenu">
<li>
<a id="ev.eventList.vadm" href="eventsList.html">All Events</a>
</li>
<li>
<a id="ev.newEventList.vadm" href="newEventsList.html">New Events</a>
</li>
</ul>
</li>
</ul>
</nav>
</div>

这是 selenium 脚本失败的部分(使用 PhantomJS):

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='EventsMenu']"))); // locating the main menu

WebElement menu = driver.findElement(By.xpath("//*[@id='EventsMenu']"));
Actions builder = new Actions(driver);
builder.moveToElement(menu).build().perform();

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='ev.newEventList.vadm']/tbody/tr[2]/td[1]/a[1]")));

WebElement menuOption = driver.findElement(By.xpath("//*[@id='ev.newEventsList.vadm']/tbody/tr[2]/td[1]/a[1]"));
menuOption.click();

最佳答案

尝试这样做并让我知道它是否有效:

    WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='EventsMenu']"))); // locating the main menu

WebElement menu = driver.findElement(By.xpath("//*[@id='EventsMenu']"));
Actions builder = new Actions(driver);
builder.moveToElement(menu).build().perform();

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='ev.newEventList.vadm']/tbody/tr[2]/td[1]/a[1]")));

WebElement menuOption = driver.findElement(By.xpath("//*[@id='ev.newEventsList.vadm']/tbody/tr[2]/td[1]/a[1]"));
builder.moveToElement(menuOption).click().build().perform();

关于java - Selenium WebDriver 将鼠标悬停在菜单上后单击下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45243106/

25 4 0