如何使用Selenium和Java将鼠标悬停在ebay.com中的某个元素上,然后单击该元素

如何使用Selenium和Java将鼠标悬停在ebay.com中的某个元素上,然后单击该元素,java,selenium,selenium-webdriver,webdriver,webdriverwait,Java,Selenium,Selenium Webdriver,Webdriver,Webdriverwait,我正在尝试使用java学习selenium。我发现鼠标悬停后很难选择元素。这是我的代码片段 driver.findElement(By.xpath("//a[contains(text(),'Tennis')]")).click() 但是,上面的代码返回错误元素不可交互 我在驱动程序findElement之前添加了线程.sleep(60000),但仍然无法单击 这是我想点击的窗口 使用操作将鼠标悬停到运动菜单,当菜单打开时,单击网球子菜单。要等待网球可点击,请使用WebDriverWait:

我正在尝试使用java学习selenium。我发现鼠标悬停后很难选择元素。这是我的代码片段

driver.findElement(By.xpath("//a[contains(text(),'Tennis')]")).click()
但是,上面的代码返回错误元素不可交互

我在驱动程序findElement之前添加了
线程.sleep(60000)
,但仍然无法单击

这是我想点击的窗口


使用
操作将鼠标悬停到运动菜单,当菜单打开时,单击网球子菜单。要等待网球可点击,请使用
WebDriverWait

WebDriverWait wait = new WebDriverWait(driver, 5);
Actions actions = new Actions(driver);

driver.get("https://www.ebay.com");

WebElement sports = driver.findElement(By.linkText("Sports"));

actions.moveToElement(sports).perform();
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Tennis"))).click();

使用
Actions
将鼠标悬停到Sports菜单,当菜单打开时,单击Tennis子菜单。要等待网球可点击,请使用
WebDriverWait

WebDriverWait wait = new WebDriverWait(driver, 5);
Actions actions = new Actions(driver);

driver.get("https://www.ebay.com");

WebElement sports = driver.findElement(By.linkText("Sports"));

actions.moveToElement(sports).perform();
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Tennis"))).click();
您需要将鼠标悬停在文本为Sports的元素上,等待文本为Tennis
元素可伸缩()

  • 代码块:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("--disable-extensions");
    //options.addArguments("disable-infobars");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.ebay.com/");
    new Actions(driver).moveToElement(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Sports")))).perform();
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Sports']//following::div[@class='hl-cat-nav__flyout']//span[text()='Other Categories']//following::ul[1]/li/a[normalize-space()='Tennis']"))).click();
    
  • 浏览器快照:

您需要将鼠标悬停在文本为Sports的元素上,等待文本为Tennis
元素可伸缩()

  • 代码块:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("--disable-extensions");
    //options.addArguments("disable-infobars");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.ebay.com/");
    new Actions(driver).moveToElement(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Sports")))).perform();
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Sports']//following::div[@class='hl-cat-nav__flyout']//span[text()='Other Categories']//following::ul[1]/li/a[normalize-space()='Tennis']"))).click();
    
  • 浏览器快照:


尝试更具体的xpath并测试您是否可以单击任何内容。例如,在易趣上找到一个id为
id
的元素,看看您是否能够单击该元素。如果可以,那么您就知道问题出在xpath选择器上。请尝试更具体的xpath并测试您是否可以单击任何内容。例如,在易趣上找到一个id为
id
的元素,看看您是否能够单击该元素。如果可以,那么您就知道问题出在xpath选择器上。