Selenium(8):对常用元素进行的操作(Selenium (8): operations on common elements)

常用元素操作

  定位到元素后,需要对元素进行操作,常见的有鼠标点击、键盘操作等,这取决于我们定位到的对象支撑哪些操作。一般来说,所有与页面交互的操作都将通过WebElement接口。

webdriver中常用的操作元素的方法有如下几个:

1、clear():清除对象的内容

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).clear()

2、send_keys():在对象上模拟按键输入

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).send_keys(‘python’)

3、click():单击对象,模拟用户点击

  driver.find_element(By.XPATH,’//a[text()=”地图”]’).click()

4、submit():提交表单,要求对象必须是表单

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).size

6、text:获取对象的文本

  driver.find_element(By.XPATH,’//a[text()=”地图”]’).text

7、get_attribute(“属性名”):获取对象的属性值

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).get_attribute(‘maxlength’)

8、is_displayed():用来判断对象是否可见,即css的display属性是否为none

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).is_displayed()

9、is_enabled():判断对象是否被禁用

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).is_enabled()

10、is_selected():判断对象是否被选中

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).is_selected()

11、tag_name:获取对象标签名称

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).tag_name

12、location:获取元素坐标

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).location

import os
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

current_path = os.path.dirname(os.path.abspath(__file__))  # 当前路径
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver路径
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie等

driver.get('https://www.baidu.com/')  # 打开网站

# send_keys()  模拟按键输入值
driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
time.sleep(2)
# clear()  清除对象的内容
driver.find_element(By.XPATH,'//input[@id="kw"]').clear()
time.sleep(2)
# click()  模拟用户点击
driver.find_element(By.XPATH,'//a[text()="地图"]').click()
# text 获取对象的文本
element_a = driver.find_element(By.XPATH,'//a[text()="地图"]')
print(element_a.text)  
# 截图
driver.find_element(By.XPATH,'//input[@id="su"]').screenshot('test_baidu.png')

element_obj = driver.find_element(By.XPATH,'//input[@id="kw"]')
print(element_obj.size)  # size 获取元素的尺寸
print(element_obj.get_attribute('maxlength'))  # get_attribute("属性名") 获取指定属性的值
print(element_obj.is_displayed())  # is_displayed() 元素是否显示
print(element_obj.is_enabled())    # is_enabled()   元素是否可用
print(element_obj.is_selected())   # is_selected()  复选框是否被选中
print(element_obj.tag_name)   # tag_name  获取元素的标签名
print(element_obj.location)   # location  获取元素左上角的坐标
————————

< strong > common element operations < / strong >

After locating an element, you need to operate the element. Common operations include mouse click, keyboard operation, etc., depending on which operations are supported by the located object. Generally speaking, all operations interacting with the page will be through the webelement interface.

< strong > there are several common methods for operating elements in webdriver: < / strong >

1. Clear (): clear the contents of the object

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).clear()

2、send_ Keys (): simulates key input on an object

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).send_keys(‘python’)

3. Click (): click an object to simulate user clicking

  driver.find_element(By.XPATH,’//a[text()=”地图”]’).click()

4. Submit(): submit a form. The object must be a form

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).size

6. Text: gets the text of the object

  driver.find_element(By.XPATH,’//a[text()=”地图”]’).text

7、get_ Attribute (“attribute name”): get the attribute value of the object

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).get_attribute(‘maxlength’)

8、is_ Displayed (): used to determine whether the object is visible, that is, whether the display attribute of CSS is none

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).is_displayed()

9、is_ Enabled(): judge whether the object is disabled

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).is_enabled()

10、is_ Selected (): judge whether the object is selected

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).is_selected()

11、tag_ Name: get object label name

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).tag_name

12. Location: get element coordinates

  driver.find_element(By.XPATH,’//input[@id=”kw”]’).location

import os
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

current_path = os.path.dirname(os.path.abspath(__file__))  # 当前路径
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver路径
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie等

driver.get('https://www.baidu.com/')  # 打开网站

# send_keys()  模拟按键输入值
driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
time.sleep(2)
# clear()  清除对象的内容
driver.find_element(By.XPATH,'//input[@id="kw"]').clear()
time.sleep(2)
# click()  模拟用户点击
driver.find_element(By.XPATH,'//a[text()="地图"]').click()
# text 获取对象的文本
element_a = driver.find_element(By.XPATH,'//a[text()="地图"]')
print(element_a.text)  
# 截图
driver.find_element(By.XPATH,'//input[@id="su"]').screenshot('test_baidu.png')

element_obj = driver.find_element(By.XPATH,'//input[@id="kw"]')
print(element_obj.size)  # size 获取元素的尺寸
print(element_obj.get_attribute('maxlength'))  # get_attribute("属性名") 获取指定属性的值
print(element_obj.is_displayed())  # is_displayed() 元素是否显示
print(element_obj.is_enabled())    # is_enabled()   元素是否可用
print(element_obj.is_selected())   # is_selected()  复选框是否被选中
print(element_obj.tag_name)   # tag_name  获取元素的标签名
print(element_obj.location)   # location  获取元素左上角的坐标