简体   繁体   中英

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

I am trying to learn selenium with java using ebay.com . I found a difficult element to select element after mouse hover. Here is my snippet code

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

However above code return error element not interactable

I have add Thread.sleep(60000) before driver.findElement and still not able to click

here is the window i wanted to click

在此处输入图片说明

Hover to Sports menu using Actions and when menu opens click to the Tennis submenu. To wait for Tennis to be clickable use 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();

You need to Mouse Hover over the element with text as Sports and wait for the elementToBeClickable() with text as Tennis and you can use the following solution:

  • Code Block:

     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(); 
  • Browser Snapshot:

易趣

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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