【问题标题】:How to click on an element which is visible after mouse hover over an element within ebay.com using Selenium and Java如何使用 Selenium 和 Java 单击鼠标悬停在 ebay.com 内的元素上后可见的元素
【发布时间】:2019-03-23 14:42:55
【问题描述】:

我正在尝试使用ebay.com 使用 java 学习 selenium。我发现鼠标悬停后很难选择元素。这是我的 sn-p 代码

driver.findElement(By.xpath("//a[contains(text(),'Tennis')]")).click()

但是上面的代码返回错误元素不可交互

我在driver.findElement 之前添加了Thread.sleep(60000),但仍然无法点击

这是我要点击的窗口

【问题讨论】:

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

标签: java selenium selenium-webdriver webdriver webdriverwait


【解决方案1】:

使用Actions将鼠标悬停到体育菜单,当菜单打开时点击网球子菜单。要等待 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();

【讨论】:

    【解决方案2】:

    您需要将鼠标悬停在文本为 Sports 的元素上,然后等待 elementToBeClickable() 与文本为 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();
      
    • 浏览器快照:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2013-05-25
      • 2013-01-19
      • 1970-01-01
      • 2011-11-08
      • 2021-07-15
      • 2021-01-18
      相关资源
      最近更新 更多