我正在尝试使用ebay.com与Java学习硒。鼠标悬停后,我发现很难选择元素。这是我的代码段
driver.findElement(By.xpath("//a[contains(text(),'Tennis')]")).click()
但是以上代码返回的错误元素不可交互
我在Thread.sleep(60000)
之前添加了driver.findElement
,但仍然无法点击
这是我要单击的窗口
答案 0 :(得分:1)
使用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();
答案 1 :(得分:1)
您需要将鼠标悬停在文本为 Sports 的元素上并将鼠标悬停在上,并等待文本为 Tennis 的elementToBeClickable()
您可以使用以下解决方案:
代码块:
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();
浏览器快照: