selenium
八大查找元素
from selenium import webdriver
from selenium.webdriver.common.by import By
# 创建一个 WebDriver 实例
driver = webdriver.Chrome()
# 打开网页
driver.get("https://www.baidu.com/")
# 使用 find_element 方法查找元素
element = driver.find_element(By.ID, "kw")
element.click()
element.send_keys("")
element.clear()
element = driver.find_element(By.NAME, "wd")
element = driver.find_element(By.CLASS_NAME, "s_ipt")
element = driver.find_element(By.TAG_NAME, "tag_name")
element = driver.find_element(By.LINK_TEXT, "新闻")
element = driver.find_element(By.PARTIAL_LINK_TEXT, "新")
element = driver.find_element(By.CSS_SELECTOR, "css_selector")
element = driver.find_element(By.XPATH, "//*[@id='kw']")
driver.find_elements()用法
获取标签属性名的值
element = driver.find_element(By.NAME, "wd")
element.get_attribute("class")
进入frame框架
# 比如<frame name="haha">
driver.switch_to.frame("haha")
出框架
driver.switch_to.default_content()
多选框
sel = Select(driver.find_element(By.NAME,"select_name"))
# 假如<option value="1"></option>
sel.select_by_value("1")
# <option value="1">哈哈</option>
sel.select_by_visible_text("哈哈")
sel.select_by_index(3)
弹窗
# alert(只有确认),confirm(有确认有取消),prompt(有确认有取消,可以输入值)
ale = driver.switch_to.alert
# 确认弹窗
ale.accept()
# 取消弹窗
ale.dismiss()
# 获取弹窗文本
strs = ale.text