Junior — Middle
What types of element search methods are available in Python for test automation or DOM work?
sobes.tech AI
Answer from AI
In Python, for test automation and working with DOM (for example, using Selenium), there are several ways to locate elements:
- By ID: the fastest and most reliable method.
element = driver.find_element(By.ID, "element_id")
- **By Tag Name**:
```python
elements = driver.find_elements(By.TAG_NAME, "div")
- By Class Name:
element = driver.find_element(By.CLASS_NAME, "class-name")
- **By Name attribute**:
```python
element = driver.find_element(By.NAME, "username")
- By CSS Selector:
element = driver.find_element(By.CSS_SELECTOR, "div.content > a.link")
- **By XPath**:
```python
element = driver.find_element(By.XPATH, "//div[@id='main']//a[text()='Click me']")
These methods allow flexible element location on the page for interaction or verification in tests.