Selenium Element Locator

Selenium Element Locators是Selenium用于指定网页元素的关键技术。定位器格式通常为:如果未指定前缀,则Selenium会根据不同的开头使用默认策略:如'dom'开头的使用DOM策略,'//'开头的使用XPath策略,其余情况则使用identifier策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Element Locators

Element Locators tell Selenium which HTML element a command refers to. The format of a locator is:

locatorType = argument

We support the following strategies for locating elements:

identifier= id
Select the element with the specified @id attribute. If no match is found, select the first element whose @name attribute is id. (This is normally the default; see below.)
id= id
Select the element with the specified @id attribute.
name= name
Select the first element with the specified @name attribute.
  • username
  • name=username
The name may optionally be followed by one or more element-filters, separated from the name by whitespace. If the filterType is not specified, value is assumed.
  • name=flavour value=chocolate
dom= javascriptExpression
Find an element by evaluating the specified string. This allows you to traverse the HTML Document Object Model using JavaScript. Note that you must not return a value in this string; simply make it the last expression in the block.
  • dom=document.forms['myForm'].myDropdown
  • dom=document.images[56]
  • dom=function foo() { return document.links[1]; }; foo();
xpath= xpathExpression
Locate an element using an XPath expression.
  • xpath=//img[@alt='The image alt text']
  • xpath=//table[@id='table1']//tr[4]/td[2]
link= textPattern
Select the link (anchor) element which contains text matching the specified pattern.
  • link=The link text
css= cssSelectorSyntax
Select the element using css selectors. Please refer to CSS2 selectors, CSS3 selectors for more information. You can also check the TestCssLocators test in the selenium test suite for an example of usage, which is included in the downloaded selenium core package.
  • css=a[href="#id3"]
  • css=span#firstChild + span
Currently the css selector locator supports all css1, css2 and css3 selectors except namespace in css3, some pseudo classes(:nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-type, :visited, :hover, :active, :focus, :indeterminate) and pseudo elements(::first-line, ::first-letter, ::selection, ::before, ::after).

Without an explicit locator prefix, Selenium uses the following default strategies:

  • dom, for locators starting with "document."
  • xpath, for locators starting with "//"
  • identifier, otherwise

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值