简体   繁体   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

I am trying to learn selenium with java using ebay.com . 我正在尝试使用ebay.com与java学习硒。 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 我在driver.findElement之前添加了Thread.sleep(60000)driver.findElement仍然无法单击

here is the window i wanted to click 这是我要单击的窗口

在此处输入图片说明

Hover to Sports menu using Actions and when menu opens click to the Tennis submenu. 悬停体育菜单使用Actions和菜单打开时,点击到网球子菜单。 To wait for Tennis to be clickable use WebDriverWait : 要等待网球可点击,请使用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: 您需要将鼠标悬停在文本为“ 体育”的元素上,并等待文本为TenniselementToBeClickable() ,然后可以使用以下解决方案:

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

易趣

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

相关问题 如何使用 selenium webdriver 悬停并单击不可见的元素? - How to Hover over and click on an invisible element using selenium webdriver? 如何鼠标悬停父元素,然后使用Selenium和Action类单击子元素 - How to mouse hover a parent element and subsequently click on child element using Selenium and Action class 如何使用 selenium java 单击未加载到 DOM 中的元素? - How to click an element which is not loaded in DOM using selenium java? 如何强制Selenium WebDriver点击当前不可见的元素? - How to force Selenium WebDriver to click on element which is not currently visible? 将鼠标悬停在元素上,然后使用Java等待Selenium WebDriver - Hover over on element and wait with Selenium WebDriver using Java 如何使用 Selenium 和 Java 单击与 HTML DOM 中的相邻元素相关的元素 - How to click an element with respect to the adjacent element within the HTML DOM using Selenium and Java 如何使用Java在Selenium WebDriver中将鼠标移到元素上来设置焦点? - How to set focus on element on moving the mouse over it in Selenium WebDriver using Java? 仅使用 Selenium 将鼠标悬停在元素上 - Hover over only on element using Selenium Selenium,Java-如何使用文本单击元素? - Selenium, Java - How to click at an element using text? 如何通过 Java 使用 Selenium 单击元素 - How to click on the element using Selenium through Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM