Software testing|Selenium element cannot be selected, the reason and solution

Selenium ElementNotSelectableExceptionexception: causes and solutions

Introduction

When conducting web automation testing, you may encounter various abnormal situations using Selenium. One of these is ElementNotSelectableExceptionan exception, which usually means something went wrong while trying to select a non-selectable element. This article details the cause of this exception, possible solutions, and provides sample code to help you better understand and handle this situation.

Abnormal

ElementNotSelectableExceptionExceptions usually occur under the following circumstances:

  1. divElement is not selective: Try an operation that selects an element that is not selective, such as using the method on a normal element .select().
  2. Element is disabled: An element may appear to be selectable, but may be set to a disabled state, preventing selection.
  3. Wrong selection operation: Using the wrong method or operation to try to select an element, such as using .select()the method to select a non-dropdown list element.

Solution

For ElementNotSelectableExceptionexceptions, you can use the following solutions:

  1. Verify element type: Make sure the element you are selecting is an optional element, such as a dropdown ( <select>element).
  2. Check element status: Before trying to select an element, make sure the element is in a selectable state, such as not disabled.
  3. Use the correct selection method: If you want to select an option in a drop-down list, make sure you use the correct method, such as .select_by_index(), .select_by_value()or .select_by_visible_text().

Solving example

The following is a sample code that demonstrates how to handle ElementNotSelectableExceptionexceptions. Suppose we want to select an option in a drop-down list, but the option may not be selected for various reasons.

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import ElementNotSelectableException

# 创建浏览器实例
driver = webdriver.Chrome()

# 打开页面
driver.get("https://example.com")

try:
    # 定位下拉列表元素
    dropdown = Select(driver.find_element_by_id("my-dropdown"))
    
    try:
        # 尝试选择选项
        dropdown.select_by_visible_text("Option 1")
    except ElementNotSelectableException:
        print("选项不可选择")

except NoSuchElementException:
    print("下拉列表元素未找到")

finally:
    driver.quit()

In this example, we first locate the dropdown list element and then try to select an option. If the option is not selectable, ElementNotSelectableExceptionthe exception will be caught and the appropriate information will be output.

Summarize

The element not selectable exception ElementNotSelectableExceptionis one of the problems you may encounter in Selenium testing, usually due to the selection operation not matching the target element. By validating element types, checking element status, and using correct selection methods, we can better handle this exception and improve the stability and reliability of automated testing.

Supongo que te gusta

Origin blog.csdn.net/Tester_muller/article/details/132541814
Recomendado
Clasificación