Operations 7 min read

Handling Non‑Select Dropdowns, Dynamic IDs, and File Uploads with Selenium Python

This article demonstrates how to automate non‑select dropdowns that allow or disallow text input, locate elements with dynamic IDs or without unique attributes using XPath, overcome click‑obstruction with JavaScript, and upload files via pywinauto when standard input elements are unavailable, all with Python Selenium code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Handling Non‑Select Dropdowns, Dynamic IDs, and File Uploads with Selenium Python

The guide explains four common challenges when using Selenium WebDriver with Python and provides concrete solutions.

1. Non‑select dropdowns – Some custom dropdowns permit typing while others do not. For the input‑enabled case, the script sends keys to trigger the smart‑suggest list and then uses ActionChains to move to the element and click the first suggestion.

brand = driver.find_element_by_css_selector('#rc_select_0')
brand.send_keys(lst[1])
time.sleep(0.5)
ActionChains(driver).move_to_element(brand).move_by_offset(0,30).click().perform()
# ActionChains(driver).move_to_element(brand).move_by_offset(0,30).context_click().perform()

For dropdowns that do not accept characters, the approach is to click the dropdown to expose all options, collect them with find_elements , iterate to match the desired text, and click the matching element via JavaScript.

driver.find_element_by_xpath("//input[starts-with(@id,'ifDangerous_productList_0')]/../..").click()
lst_dangerous = driver.find_elements_by_xpath("//div[starts-with(@id,'ifDangerous_productList_0')]/../div[2]/div[1]/div[1]/div[1]/div")
for dangerous in lst_dangerous:
    if lst[12] == dangerous.get_attribute('title'):
        driver.execute_script('arguments[0].click()', dangerous)

2. Locating elements with dynamic IDs or without unique attributes – When IDs change, use stable parts of the ID together with XPath axes (parent, sibling) to reach the target element.

driver.find_element_by_xpath("//div[@id='xxx']/A")  # child A of a known parent
driver.find_element_by_xpath("//div[@id='xxx']/..")   # parent of the known element
driver.find_element_by_xpath("//div[@id='xxx']/../A") # sibling A via parent

If an element lacks a unique attribute, relative XPath or CSS selectors based on surrounding stable elements are preferred over absolute positions, which may shift due to hidden nodes.

3. Solving click‑obstruction with JavaScript – When a normal Selenium click is blocked, execute a JavaScript click on the element.

ele = driver.find_element_by_xpath("//input[starts-with(@id,'chemicaltype_productList_0')]/../..")
driver.execute_script('arguments[0].click()', ele)

4. Uploading files when the control is not an input element – Use send_keys for regular file inputs; for custom upload buttons that open a native OS dialog, employ the pywinauto library to interact with the dialog.

# Click the custom upload button
upload1 = driver.find_element_by_xpath("//div[text()='MSDS文件:']//../div[2]/span[1]/div[1]/span[1]/button[1]")
driver.execute_script('arguments[0].click()', upload1)
# Control the Windows file dialog with pywinauto
app = pywinauto.Desktop()
win = app['文件上传']  # name varies by browser (e.g., "打开" in Chrome)
win['Edit'].type_keys(path + '\\' + file)
win['Button'].click()

Two practical reminders: the dialog window title differs across browsers (Firefox uses “上传文件”, Chrome uses “打开”), and security tools like 360 Security Guard can interfere with the automation.

The article concludes with links to additional Python resources and a QR code for a free Python course.

File Uploadxpathwebdriver
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.