selenium获取iframe下的dropdown封装的动态下拉框元素
时间: 2024-05-14 10:14:31 浏览: 14
以下是使用Selenium获取iframe下的dropdown封装的动态下拉框元素的示例代码:
```python
from selenium.webdriver.support.ui import Select
class DynamicDropdown:
def __init__(self, driver, iframe_id, dropdown_id):
self.driver = driver
self.iframe_id = iframe_id
self.dropdown_id = dropdown_id
def select_option_by_visible_text(self, option_text):
self.driver.switch_to.frame(self.iframe_id)
dropdown = Select(self.driver.find_element_by_id(self.dropdown_id))
dropdown.select_by_visible_text(option_text)
self.driver.switch_to.default_content()
```
使用方法:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# switch to iframe
iframe_id = "frameId"
driver.switch_to.frame(iframe_id)
# initialize dynamic dropdown object
dropdown_id = "dropdownId"
dynamic_dropdown = DynamicDropdown(driver, iframe_id, dropdown_id)
# select option by visible text
option_text = "Option 1"
dynamic_dropdown.select_option_by_visible_text(option_text)
# switch back to default content
driver.switch_to.default_content()
```