繁体   English   中英

在使用Selenium和Java将鼠标悬停在ebay.com中的元素上之后,如何单击可见的元素

[英]How to click on an element which is visible after mouse hover over an element within ebay.com using Selenium and Java

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

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

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

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

这是我要单击的窗口

在此处输入图片说明

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

您需要将鼠标悬停在文本为“ 体育”的元素上,并等待文本为TenniselementToBeClickable() ,然后可以使用以下解决方案:

  • 代码块:

     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(); 
  • 浏览器快照:

易趣

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM