Skip to content

Latest commit

 

History

History
331 lines (257 loc) · 11.1 KB

File metadata and controls

331 lines (257 loc) · 11.1 KB
title 关于网络元素的信息
linkTitle 信息
weight 4
description 元素相关的知识.

您可以查询有关特定元素的许多详细信息。

是否显示

此方法用于检查连接的元素是否正确显示在网页上. 返回一个 Boolean 值, 如果连接的元素显示在当前的浏览器上下文中,则为True,否则返回false。

此功能于W3C规范中提及, 但由于无法覆盖所有潜在条件而无法定义。 因此,Selenium不能期望驱动程序直接实现这种功能,现在依赖于直接执行大量JavaScript函数。 这个函数对一个元素的性质和在树中的关系做了许多近似的判断,以返回一个值。

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L20-L24" >}} {{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L12-L15" >}} {{< /tab >}} {{< tab header="CSharp" text=true >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L18-L22" >}} {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="/examples/ruby/spec/elements/information_spec.rb#L12">}} {{< /tab >}} {{< tab header="JavaScript" text=true >}} {{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L16-L17">}} {{< /tab >}} {{< tab header="Kotlin" >}}

//navigates to url driver.get("https://www.selenium.dev/selenium/web/inputs.html")

//returns true if element is displayed else returns false val flag = driver.findElement(By.name("email_input")).isDisplayed()

{{< /tab >}} {{< /tabpane >}}

是否启用

此方法用于检查所连接的元素在网页上是启用还是禁用状态。 返回一个布尔值,如果在当前浏览上下文中是 启用 状态,则返回 true,否则返回 false

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L27-L29" >}} {{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L19" >}} {{< /tab >}} {{< tab header="CSharp" text=true >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L25-L27" >}} {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="/examples/ruby/spec/elements/information_spec.rb#L17">}} {{< /tab >}} {{< tab header="JavaScript" text=true >}} {{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L23-L24">}} {{< /tab >}} {{< tab header="Kotlin" >}}

//navigates to url driver.get("https://www.selenium.dev/selenium/web/inputs.html")

//returns true if element is enabled else returns false val attr = driver.findElement(By.name("button_input")).isEnabled()

{{< /tab >}} {{< /tabpane >}}

是否被选定

此方法确认相关的元素是否 已选定,常用于复选框、单选框、输入框和选择元素中。

该方法返回一个布尔值,如果在当前浏览上下文中 选择了 引用的元素,则返回 True,否则返回 False

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L32-L34" >}} {{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L23" >}} {{< /tab >}} {{< tab header="CSharp" text=true >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L30-L32" >}} {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="/examples/ruby/spec/elements/information_spec.rb#L22">}} {{< /tab >}} {{< tab header="JavaScript" text=true >}} {{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L30-L31">}} {{< /tab >}} {{< tab header="Kotlin" >}}

//navigates to url driver.get("https://www.selenium.dev/selenium/web/inputs.html")

//returns true if element is checked else returns false val attr = driver.findElement(By.name("checkbox_input")).isSelected()

{{< /tab >}} {{< /tabpane >}}

获取元素标签名

此方法用于获取在当前浏览上下文中具有焦点的被引用元素的TagName

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L37-L39" >}} {{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L27" >}} {{< /tab >}} {{< tab header="CSharp" text=true >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L35-L37" >}} {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="/examples/ruby/spec/elements/information_spec.rb#L27">}} {{< /tab >}} {{< tab header="JavaScript" text=true >}} {{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L37-L38">}} {{< /tab >}} {{< tab header="Kotlin" >}}

//navigates to url driver.get("https://www.selenium.dev/selenium/web/inputs.html")

//returns TagName of the element val attr = driver.findElement(By.name("email_input")).getTagName()

{{< /tab >}} {{< /tabpane >}}

位置和大小

用于获取参照元素的尺寸和坐标。

提取的数据主体包含以下详细信息:

  • 元素左上角的X轴位置
  • 元素左上角的y轴位置
  • 元素的高度
  • 元素的宽度

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L42-L44" >}} {{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L31" >}} {{< /tab >}} {{< tab header="CSharp" text=true >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L40-L43" >}} {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="/examples/ruby/spec/elements/information_spec.rb#L32">}} {{< /tab >}} {{< tab header="JavaScript" text=true >}} {{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L45">}} {{< /tab >}} {{< tab header="Kotlin" >}}

// Navigate to url driver.get("https://www.selenium.dev/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())

{{< /tab >}} {{< /tabpane >}}

获取元素CSS值

获取当前浏览上下文中元素的特定计算样式属性的值。

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L49-L50" >}} {{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L35-L37" >}} {{< /tab >}} {{< tab header="CSharp" text=true >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L49-L50" >}} {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="/examples/ruby/spec/elements/information_spec.rb#L38">}} {{< /tab >}} {{< tab header="JavaScript" text=true >}} {{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L76-L78">}} {{< /tab >}} {{< tab header="Kotlin" >}}

// Navigate to Url driver.get("https://www.selenium.dev/selenium/web/colorPage.html")

// Retrieves the computed style property 'color' of linktext val cssValue = driver.findElement(By.id("namedColor")).getCssValue("background-color")

{{< /tab >}} {{< /tabpane >}}

文本内容

获取特定元素渲染后的文本内容。

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L54-L56" >}} {{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L41" >}} {{< /tab >}} {{< tab header="CSharp" text=true >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L53-L55" >}} {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="/examples/ruby/spec/elements/information_spec.rb#L43">}} {{< /tab >}} {{< tab header="JavaScript" text=true >}} {{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L84-L86">}} {{< /tab >}} {{< tab header="Kotlin" >}}

// Navigate to URL driver.get("https://www.selenium.dev/selenium/web/linked_image.html")

// retrieves the text of the element val text = driver.findElement(By.id("justanotherlink")).getText()

{{< /tab >}} {{< /tabpane >}}

获取特性或属性

获取与 DOM 属性关联的运行时的值。 它返回与该元素的 DOM 特性或属性关联的数据。

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L60-L64" >}} {{< /tab >}} {{< tab header="Python" text=true >}} {{< gh-codeblock path="/examples/python/tests/elements/test_information.py#L44-L46" >}} {{< /tab >}} {{< tab header="CSharp" text=true >}} {{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/InformationTest.cs#L58-L62" >}} {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="/examples/ruby/spec/elements/information_spec.rb#L48">}} {{< /tab >}} {{< tab header="JavaScript" text=true >}} {{< gh-codeblock path="/examples/javascript/test/elements/information.spec.js#L55-L59">}} {{< /tab >}} {{< tab header="Kotlin" >}}

// Navigate to URL driver.get("https://www.selenium.dev/selenium/web/inputs.html")

//fetch the value property associated with the textbox val attr = driver.findElement(By.name("email_input")).getAttribute("value")

{{< /tab >}} {{< /tabpane >}}

Get Dom Property

This method retrieves the value of a specific DOM property of a web element.

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L67-L69" >}} {{< /tab >}} {{< tab header="Python" text=true >}}

{{< /tab >}} {{< tab header="CSharp" text=true >}}

{{< /tab >}} {{< tab header="Ruby" text=true >}}

{{< /tab >}} {{< tab header="JavaScript" text=true >}}

{{< /tab >}} {{< tab header="Kotlin" >}}

{{< /tab >}} {{< /tabpane >}}

Get Dom Attribute

This method retrieves the value of a specific HTML attribute of a web element

{{< tabpane langEqualsHeader=true >}} {{< tab header="Java" text=true >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/InformationTest.java#L71-L73" >}} {{< /tab >}} {{< tab header="Python" text=true >}}

{{< /tab >}} {{< tab header="CSharp" text=true >}}

{{< /tab >}} {{< tab header="Ruby" text=true >}}

{{< /tab >}} {{< tab header="JavaScript" text=true >}}

{{< /tab >}} {{< tab header="Kotlin" >}}

{{< /tab >}} {{< /tabpane >}}