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

如何通过findElement(By.xpath("locator"))从<ul>选<li>?弹窗选Mr.求助

Fixing Automation to Select 'Mr.' in the Div Popup Dropdown

Got it, let's troubleshoot why your automation isn't successfully selecting the 'Mr.' option in that custom div dropdown popup. Based on the HTML snippet you shared, this looks like a non-native dropdown (built with divs/li elements instead of a standard <select>), so we need to adjust our approach from typical select-element handling. Here are the key steps to fix this:

1. Make Sure You're Waiting for the Popup to Fully Load

Dynamic popups like this don't appear instantly—your automation might be trying to interact with the options before they're rendered. Always use explicit waits to ensure the popup is visible and ready:

# Example with Selenium
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wait for the popup menu to become visible
popup_menu = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.CLASS_NAME, "uiMenuList"))
)

2. First Trigger the Dropdown to Open

You can't select an option if the popup isn't open! First, click the trigger element that opens the dropdown. From your HTML, the popup has aria-labelledby="170:2436;a"—that value is likely the ID of the trigger button. Target that first:

# Click the dropdown trigger to reveal the options
dropdown_trigger = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "170:2436;a"))
)
dropdown_trigger.click()

3. Target the 'Mr.' Option Correctly

Since this is a custom dropdown, forget about using the Select class. Instead, locate the option directly by its text or position:

  • By text (most reliable if the text doesn't change):
mr_option = WebDriverWait(popup_menu, 10).until(
    EC.element_to_be_clickable((By.XPATH, ".//*[text()='Mr.']"))
)
mr_option.click()
  • By position (since it's the second option, after --None--):
# Target the second child element in the popup menu
mr_option = WebDriverWait(popup_menu, 10).until(
    EC.element_to_be_clickable((By.XPATH, ".//*[position()=2]"))
)
mr_option.click()

4. If Regular Clicks Fail, Use JavaScript Injection

Sometimes custom UI elements have overlapping layers or disabled states that block regular clicks. If the above doesn't work, try clicking via JavaScript:

driver.execute_script("arguments[0].click();", mr_option)

Quick Checks if It Still Fails

  • Verify the full HTML structure: Are the options wrapped in <li> or <div> tags? Adjust your XPath to match the actual tag (e.g., .//li[text()='Mr.'] instead of .//*).
  • Check for iframes: If the popup is inside an iframe, you'll need to switch to the iframe first before interacting with elements.
  • Dynamic IDs: If the IDs like 170:2436;a change on page reload, use a more stable locator (e.g., By.CSS_SELECTOR targeting a class or attribute that doesn't change).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:20:05