如何将鼠标悬停在父元素上,然后使用 Selenium 和 Action 单击子元素 class
How to mouse hover a parent element and subsequently click on child element using Selenium and Action class
我编写了一个测试,将鼠标悬停在下方有 link 的元素上,然后单击子元素。我不断收到 NullPointerException。它以前工作过,然后又停止工作了。
Actions mouseHover = new Actions(driver);
mouseHover.moveToElement(ParentElement);
mouseHover.moveToElement(subElement);
mouseHover.click(subElement);
它可能试图在元素出现之前点击它。在移动到子元素之前尝试使用网络驱动程序等待。 (因为它以前有效,我想这应该是问题所在)
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.LOCATOR("subelement")));
它看起来像,
Actions mouseHover = new Actions(driver);
mouseHover.moveToElement(ParentElement);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.LOCATOR("subelement")));
mouseHover.moveToElement(subElement);
mouseHover.click(subElement);
干杯
根据您的代码尝试,您没有为 调用 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 实例,即 driver 无法从这部分代码访问。该问题可能与 driver 为 null 有关,因为您没有在 [=32] 中扩展 Base
class =]测试class。确保 驱动程序 可访问。
相关讨论:
- What is this Error: at com.google.common.base.Preconditions.checkNotNull
- Why does the NullPointerException occur on the Selenium driver?
我编写了一个测试,将鼠标悬停在下方有 link 的元素上,然后单击子元素。我不断收到 NullPointerException。它以前工作过,然后又停止工作了。
Actions mouseHover = new Actions(driver);
mouseHover.moveToElement(ParentElement);
mouseHover.moveToElement(subElement);
mouseHover.click(subElement);
它可能试图在元素出现之前点击它。在移动到子元素之前尝试使用网络驱动程序等待。 (因为它以前有效,我想这应该是问题所在)
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.LOCATOR("subelement")));
它看起来像,
Actions mouseHover = new Actions(driver);
mouseHover.moveToElement(ParentElement);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.LOCATOR("subelement")));
mouseHover.moveToElement(subElement);
mouseHover.click(subElement);
干杯
根据您的代码尝试,您没有为 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 实例,即 driver 无法从这部分代码访问。该问题可能与 driver 为 null 有关,因为您没有在 [=32] 中扩展 Base
class =]测试class。确保 驱动程序 可访问。
相关讨论:
- What is this Error: at com.google.common.base.Preconditions.checkNotNull
- Why does the NullPointerException occur on the Selenium driver?