element = driver.find_element_by_id("passwd-id") #通过id获取元素
element = driver.find_element_by_name("passwd") #通过name获取元素
element = driver.find_element_by_xpath("//input[@id='passwd-id']") #通过使用xpath匹配获取元素
- 定位一个元素:
- find_element_by_id
- find_element_by_name
- find_element_by_xpath
- find_element_by_link_text
- find_element_by_partial_link_text
- find_element_by_tag_name
- find_element_by_class_name
- find_element_by_css_selector
- 定位多个元素:
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
在输入框中写入数据:elem.send_keys("pycon")
from selenium.webdriver.support.ui import Select #导入Select
select = Select(driver.find_element_by_name('name')) #通过Select来定义该元素是下拉框
select.select_by_index(index) #通过下拉元素的位置来选择
select.select_by_visible_text("text") #通过下拉元素的内容来选择
select.select_by_value(value) #通过下拉元素的取值来选择
点击
elem.click()