You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium Java点击网站Requests链接失败,求解决方案

Fixing Selenium Java Issue: Clicking "Requests" & Accessing Submenu

I’ve looked at your problem and the page source, let’s work through this step by step to get your code working correctly:

First, Fix the XPath Typo & Target the Right Element

Your earlier TimeoutException was purely due to a typo (sidebar-memu instead of sidebar-menu)—easy fix! But even with that corrected, we need to make sure we’re clicking the right part of the "Requests" menu to trigger the dropdown.

Instead of using the fragile absolute XPath from xpathfinder, let’s use a more stable locator. Since the parent <li> has a unique ID tv_3, we can target the <a> tag inside it (which is what actually triggers the dropdown):

// Wait for the Requests menu anchor to be clickable, then click it
WebElement requestsAnchor = new WebDriverWait(driver, 20)
        .until(ExpectedConditions.elementToBeClickable(
                By.xpath("//li[@id='tv_3']/a")
        ));
requestsAnchor.click();

If you prefer to target the "Requests" text directly, this XPath works too (and is more readable):

WebElement requestsText = new WebDriverWait(driver, 20)
        .until(ExpectedConditions.elementToBeClickable(
                By.xpath("//span[text()='Requests']")
        ));
requestsText.click();

Wait for the Dropdown to Expand Before Clicking the Submenu

The NoSuchElementException for "Security Test Requests" happens because you’re trying to click it before the dropdown (treeview-menu) has fully expanded. We need to wait for that submenu to become visible first:

// Wait for the dropdown menu to appear
new WebDriverWait(driver, 20)
        .until(ExpectedConditions.visibilityOfElementLocated(
                By.className("treeview-menu")
        ));

// Now safely click the Security Test Requests link
WebElement securityTestLink = new WebDriverWait(driver, 20)
        .until(ExpectedConditions.elementToBeClickable(
                By.linkText("Security Test Requests")
        ));
securityTestLink.click();

Fallback: Use JavaScript Click if Regular Click Doesn’t Work

Sometimes, regular Selenium clicks don’t trigger JavaScript-based dropdowns (if the site uses custom event listeners). If the above steps still don’t expand the menu, try using JavascriptExecutor:

WebElement requestsAnchor = new WebDriverWait(driver, 20)
        .until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("//li[@id='tv_3']/a")
        ));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", requestsAnchor);

// Then wait for the dropdown and click the submenu as before

What Was Wrong With Your Original Code?

  • You clicked the <i> icon inside the Requests link instead of the actual anchor tag—this might not have triggered the dropdown’s expand event.
  • The typo in your XPath (sidebar-memu) caused the timeout, since Selenium couldn’t locate the element.
  • You didn’t wait for the dropdown to expand before trying to click the submenu, so the element wasn’t present yet.

内容的提问来源于stack exchange,提问作者Ashar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 11:32:52