关于 Web 元素的信息
您可以了解关于元素的什么信息。
您可以查询有关特定元素的许多详细信息。
是否显示
此方法用于检查连接的元素是否在网页上显示。返回一个 Boolean
值,如果连接的元素在当前浏览上下文中显示,则返回 True,否则返回 false。
此功能在 中提到,但由于 不可能涵盖所有潜在条件,因此未由 w3c 规范定义。 因此,Selenium 不能期望驱动程序直接实现此功能,现在依赖于直接执行大型 JavaScript 函数。 此函数会对元素的性质和树中的关系进行许多近似,以返回一个值。
driver.get("https://seleniumcn.cn/selenium/web/inputs.html");
// isDisplayed
// Get boolean value for is element display
boolean isEmailVisible = driver.findElement(By.name("email_input")).isDisplayed();
assertEquals(isEmailVisible,true);
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
# isDisplayed
is_email_visible = driver.find_element(By.NAME, "email_input").is_displayed()
// Navigate to Url
driver.Url= "https://seleniumcn.cn/selenium/web/inputs.html";
// isDisplayed
// Get boolean value for is element display
bool isEmailVisible = driver.FindElement(By.Name("email_input")).Displayed;
Assert.AreEqual(isEmailVisible, true);
displayed_value = driver.find_element(name: 'email_input').displayed?
// Resolves Promise and returns boolean value
let result = await driver.findElement(By.name("email_input")).isDisplayed();
//navigates to url
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
//returns true if element is displayed else returns false
val flag = driver.findElement(By.name("email_input")).isDisplayed()
是否启用
此方法用于检查连接的元素在网页上是否已启用或禁用。返回一个布尔值,如果连接的元素在当前浏览上下文中是启用的,则返回 True,否则返回 false。
//isEnabled
//returns true if element is enabled else returns false
boolean isEnabledButton = driver.findElement(By.name("button_input")).isEnabled();
assertEquals(isEnabledButton,true);
is_enabled_button = driver.find_element(By.NAME, "button_input").is_enabled()
//isEnabled
//returns true if element is enabled else returns false
bool isEnabledButton = driver.FindElement(By.Name("button_input")).Enabled;
Assert.AreEqual(isEnabledButton, true);
enabled_value = driver.find_element(name: 'email_input').enabled?
// Resolves Promise and returns boolean value
let element = await driver.findElement(By.name("button_input")).isEnabled();
//navigates to url
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
//returns true if element is enabled else returns false
val attr = driver.findElement(By.name("button_input")).isEnabled()
是否选中
此方法确定引用的元素是否被*选中*。此方法广泛用于复选框、单选按钮、输入元素和选项元素。
返回一个布尔值,如果引用的元素在当前浏览上下文中是选中的,则返回 True,否则返回 false。
//isSelected
//returns true if element is checked else returns false
boolean isSelectedCheck = driver.findElement(By.name("checkbox_input")).isSelected();
assertEquals(isSelectedCheck,true);
is_selected_check = driver.find_element(By.NAME, "checkbox_input").is_selected()
//isSelected
//returns true if element is checked else returns false
bool isSelectedCheck = driver.FindElement(By.Name("checkbox_input")).Selected;
Assert.AreEqual(isSelectedCheck, true);
selected_value = driver.find_element(name: 'email_input').selected?
// Returns true if element ins checked else returns false
let isSelected = await driver.findElement(By.name("checkbox_input")).isSelected();
//navigates to url
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
//returns true if element is checked else returns false
val attr = driver.findElement(By.name("checkbox_input")).isSelected()
标签名称
它用于获取当前浏览上下文中具有焦点的引用元素的 TagName。
//TagName
//returns TagName of the element
String tagNameInp = driver.findElement(By.name("email_input")).getTagName();
assertEquals(tagNameInp,"input");
tag_name_inp = driver.find_element(By.NAME, "email_input").tag_name
//TagName
//returns TagName of the element
string tagNameInp = driver.FindElement(By.Name("email_input")).TagName;
Assert.AreEqual(tagNameInp, "input");
tag_name = driver.find_element(name: 'email_input').tag_name
// Returns TagName of the element
let value = await driver.findElement(By.name('email_input')).getTagName();
//navigates to url
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
//returns TagName of the element
val attr = driver.findElement(By.name("email_input")).getTagName()
大小和位置
它用于获取引用元素的尺寸和坐标。
获取的数据正文包含以下详细信息
- 元素左上角起点的 X 轴位置
- 元素左上角起点的 Y 轴位置
- 元素的高度
- 元素的宽度
//GetRect
// Returns height, width, x and y coordinates referenced element
Rectangle res = driver.findElement(By.name("range_input")).getRect();
// Rectangle class provides getX,getY, getWidth, getHeight methods
assertEquals(res.getX(),10);
rect = driver.find_element(By.NAME, "range_input").rect
//Get Location and Size
//Get Location
IWebElement rangeElement = driver.FindElement(By.Name("range_input"));
Point point = rangeElement.Location;
Assert.IsNotNull(point.X);
//Get Size
int height=rangeElement.Size.Height;
Assert.IsNotNull(height);
size = driver.find_element(name: 'email_input').size
let object = await driver.findElement(By.name('range_input')).getRect();
// Navigate to url
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
// Returns height, width, x and y coordinates referenced element
val res = driver.findElement(By.name("range_input")).rect
// Rectangle class provides getX,getY, getWidth, getHeight methods
println(res.getX())
获取 CSS 值
检索当前浏览上下文中元素的指定计算样式属性的值。
// Retrieves the computed style property 'font-size' of field
String cssValue = driver.findElement(By.name("color_input")).getCssValue("font-size");
assertEquals(cssValue, "13.3333px");
css_value = driver.find_element(By.NAME, "color_input").value_of_css_property(
"font-size"
)
// Retrieves the computed style property 'font-size' of field
string cssValue = driver.FindElement(By.Name("color_input")).GetCssValue("font-size");
Assert.AreEqual(cssValue, "13.3333px");
css_value = driver.find_element(name: 'email_input').css_value('background-color')
await driver.get('https://seleniumcn.cn/selenium/web/colorPage.html');
// Returns background color of the element
let value = await driver.findElement(By.id('namedColor')).getCssValue('background-color');
// Navigate to Url
driver.get("https://seleniumcn.cn/selenium/web/colorPage.html")
// Retrieves the computed style property 'color' of linktext
val cssValue = driver.findElement(By.id("namedColor")).getCssValue("background-color")
文本内容
检索指定元素的呈现文本。
//GetText
// Retrieves the text of the element
String text = driver.findElement(By.tagName("h1")).getText();
assertEquals(text, "Testing Inputs");
text = driver.find_element(By.TAG_NAME, "h1").text
//GetText
// Retrieves the text of the element
string text = driver.FindElement(By.TagName("h1")).Text;
Assert.AreEqual(text, "Testing Inputs");
text = driver.find_element(xpath: '//h1').text
await driver.get('https://seleniumcn.cn/selenium/web/linked_image.html');
// Returns text of the element
let text = await driver.findElement(By.id('justanotherLink')).getText();
// Navigate to URL
driver.get("https://seleniumcn.cn/selenium/web/linked_image.html")
// retrieves the text of the element
val text = driver.findElement(By.id("justanotherlink")).getText()
获取属性或属性
获取与 DOM 属性关联的运行时值。它返回与元素的 DOM 属性或属性关联的数据。
//FetchAttributes
//identify the email text box
WebElement emailTxt = driver.findElement(By.name(("email_input")));
//fetch the value property associated with the textbox
String valueInfo = emailTxt.getAttribute("value");
assertEquals(valueInfo,"admin@localhost");
# FetchAttributes
email_txt = driver.find_element(By.NAME, "email_input")
value_info = email_txt.get_attribute("value")
//FetchAttributes
//identify the email text box
IWebElement emailTxt = driver.FindElement(By.Name("email_input"));
//fetch the value property associated with the textbox
string valueInfo = emailTxt.GetAttribute("value");
Assert.AreEqual(valueInfo, "admin@localhost");
attribute_value = driver.find_element(name: 'number_input').attribute('value')
// identify the email text box
const emailElement = await driver.findElement(By.xpath('//input[@name="email_input"]'));
//fetch the attribute "name" associated with the textbox
const nameAttribute = await emailElement.getAttribute("name");
// Navigate to URL
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
//fetch the value property associated with the textbox
val attr = driver.findElement(By.name("email_input")).getAttribute("value")