gpt4 book ai didi

java - 如何将鼠标悬停在父元素上,然后使用 Selenium 和 Action 类单击子元素

转载 作者:搜寻专家 更新时间:2023-11-01 03:46:18 24 4
gpt4 key购买 nike

我编写了一个测试,将鼠标悬停在下面有链接的元素上,然后单击子元素。我不断收到 NullPointerException。它以前工作过,然后又停止工作了。

Actions mouseHover = new Actions(driver);
mouseHover.moveToElement(ParentElement);
mouseHover.moveToElement(subElement);
mouseHover.click(subElement);

最佳答案

根据您的代码尝试,您没有为 Mouse Hover 调用 perform() 方法| .您需要为元素引入WebDriverWait,可以使用以下解决方案:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
//other lines of code
Actions mouseHover = new Actions(driver);
mouseHover.moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOf(ParentElement)))).perform();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(subElement))).click();

更新

因为您仍然看到错误:

 java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:882) at org.openqa.selenium.interactions.Actions.<init>(Actions.java:68)

这意味着 WebDriver 实例,即 驱动程序 无法从这部分代码访问。该问题可能与 drivernull 有关,因为您没有在 Test 类中扩展 Base 类。确保驱动程序可访问。

相关讨论:

关于java - 如何将鼠标悬停在父元素上,然后使用 Selenium 和 Action 类单击子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52384816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com