selenium操作的基本方法
from selenium.webdriver.common.by import By
根据id选择元素,返回的就是该元素对应的WebElement对象
element=wd.find_element(By.ID,'kw')
# 通过该 WebElement对象,就可以对页面元素进行操作了,比如输入字符串到这个输入框里
element.send_keys('通讯')
特别注意:Selenium 升级到版本 4 以后, 这种 find_element_by* 方法都作为过期不赞成的写法
新版本的建议写法为
from selenium.webdriver.common.by import By
初始化代码 ....
wd.find_element(By.ID, 'username').send_keys('mytest')
wd.find_element(By.CLASS_NAME, 'password').send_keys('mytest')
wd.find_element(By.TAG_NAME, 'input').send_keys('mytest')
wd.find_element(By.CSS_SELECTOR,'button[type=submit]').click()
如果使用 wd.find_element (注意少了一个s) 方法, 就只会返回 第一个 元素
find_elements 返回的是找到的符合条件的所有元素 (这里有3个元素), 放在一个 列表中返回。
# 里面 都是class 属性值为 animal的元素对应的 WebElement对象
elements = wd.find_elements(By.CLASS_NAME, 'animal')
# 取出列表中的每个 WebElement对象,打印出其text属性的值 # text属性就是该 WebElement对象对应的元素在网页中的文本内容
for element in elements:
print(element.text)
张三
值得注意的是,这里 span元素 有两个class属性,分别 是 chinese 和 student, 而不是一个 名为 chinese student 的属性。我们要用代码选择这个元素,可以指定任意一个class 属性值,都可以选择到这个元素,
element = wd.find_elements(By.CLASS_NAME,'chinese')
但是不能写成
element = wd.find_elements(By.CLASS_NAME,'chinese student')
值得注意的是
find_element 和 find_elements 的区别
使用 find_elements 选择的是符合条件的 所有 元素, 如果没有符合条件的元素, 返回空列表使用
find_element 选择的是符合条件的 第一个 元素, 如果没有符合条件的元素, 抛出 NoSuchElementException 异常
等待元素
当发现元素没有找到的时候, 并不立即返回 找不到元素的错误
Selenium 的 Webdriver 对象 有个方法叫 implicitly_wait ,可以称之为 隐式等待 ,或者 全局等待 。
wd.implicitly_wait(10)
显示等待
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"kw"))).send_keys("好好学习")
输入框
element = wd.find_element(By.ID, "input1")
element.clear()
清除输入框已有的字符串
element.send_keys('test') # 输入新字符串
获取元素信息
获取元素的文本内容
element = wd.find_element(By.ID, 'animal')
print(element.text)
``
获取元素属性
print(element.get_attribute('class'))
获取整个元素对应的HTML
element.get_attribute('innerHTML')
获取输入框里面的文字
print(element.get_attribute('value')) # 获取输入框中的文本
值得注意的是
可以使用 element.get_attribute('innerText') ,或者 element.get_attribute('textContent')
使用 innerText 和 textContent 的区别是,前者只显示元素可见文本内容,后者显示所有内容(包括display属性为none的部分)
frame切换/窗口切换
这个 iframe 元素非常的特殊, 在html语法中,frame 元素 或者iframe元素的内部 会包含一个 被嵌入的 另一份html文档。
wd.switch_to.frame(frame_reference)
如:wd.switch_to.frame('frame1')
如:wd.switch_to.frame('innerFrame')
也可以写成wd.switch_to.frame(wd.find_element(By.TAG_NAME, "iframe"))
切回主html
wd.switch_to.default_content()
切换打新的窗口
wd.switch_to.window(handle)
可以保存句柄,用来切换窗口
mainWindow = wd.current_window_handle
wd.switch_to.window(mainWindow)
选择框
radio框
wd.find_element(By.CSS_SELECTOR, '#s_radio input[value="小雷老师"]').click()
checkbox框
先把 已经选中的选项全部点击一下
elements = wd.find_elements(By.CSS_SELECTOR, '#s_checkbox input[name="teacher"]:checked')
for element in elements:
element.click()
# 再点击 小雷老师
wd.find_element(By.CSS_SELECTOR, "#s_checkbox input[value='小雷老师']").click()
select框
from selenium.webdriver.support.ui import Select
创建Select对象
select = Select(wd.find_element(By.ID, "ss_single"))
select.select_by_visible_text("小雷老师")
Select多选框
导入Select类
from selenium.webdriver.support.ui import Select
select = Select(wd.find_element(By.ID, "ss_multi"))
select.deselect_all()
select.select_by_visible_text("小雷老师")
select.select_by_visible_text("小凯老师")
鼠标移动\拖拽
from selenium.webdriver.common.action_chains import ActionChains
ac = ActionChains(driver)
鼠标移动到 元素上
ac.move_to_element( driver.find_element(By.CSS_SELECTOR, '[name="tj_briicon"]')).perform()
执行javascript
nextPageButtonDisabled = driver.execute_script()
Alert
driver.switch_to.alert.accept()
driver.switch_to.alert.text
Confirm
driver.switch_to.alert.accept()
driver.switch_to.alert.dismiss()
Prompt
driver.switch_to.alert.send_keys()
其他技巧
#获取窗口大小
driver.get_window_size()
#改变窗口大小
driver.set_window_size(x, y)
#获取当前窗口URL地址
driver.current_url
#刷新当前窗口URL
driver.refresh()
#截屏
driver.get_screenshot_as_file('1.png')
#上传文件
ele = wd.find_element(By.CSS_SELECTOR, 'input[type=file]')
ele.send_keys(r'h:\g02.png')