selenium选择省市区
时间: 2023-11-29 22:06:11 浏览: 47
以下是使用Selenium选择省市区的示例代码:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
# 启动Chrome浏览器
browser = webdriver.Chrome()
# 打开网页
browser.get('https://example.com')
# 选择省份
province_select = Select(browser.find_element(By.ID, 'province'))
province_select.select_by_visible_text('广东省')
# 选择城市
city_select = Select(browser.find_element(By.ID, 'city'))
city_select.select_by_visible_text('深圳市')
# 选择区县
district_select = Select(browser.find_element(By.ID, 'district'))
district_select.select_by_visible_text('南山区')
```
上述代码中,我们首先启动了Chrome浏览器,并打开了一个网页。然后,我们使用`Select`类来选择省、市、区县。`Select`类需要传入一个`WebElement`对象,我们可以使用`find_element`方法来获取该对象。`select_by_visible_text`方法可以根据选项的可见文本来选择选项。