Selenium Tips - CSS定位元素

Selenium Tips - CSS定位元素
原文:
http://sauceio.com/index.php/2010/01/selenium-totw-css-selectors-in-selenium-demystified/

Following my previous TOTW about improving your locators, this blog post will show you some advanced CSS rules and pseudo-classes that will help you move your XPATH locators to CSS, a native approach on all browsers.

Next sibling

Our first example is useful for navigating lists of elements, such as forms or ul items. The next sibling will tell selenium to find the next adjacent element on the page that’s inside the same parent. Let’s show an example using a form to select the field after username.

</input></input>

Let’s write a css selector that will choose the input field after “username”. This will select the “alias” input, or will select a different element if the form is reordered.

css=form input.username + inputAttribute values

If you don’t care about the ordering of child elements, you can use an attribute selector in selenium to choose elements based on any attribute value. A good example would be choosing the ‘username’ element of the form without adding a class.

</input></input></input></input>

We can easily select the username element without adding a class or an id to the element.

css=form input[name='username']

We can even chain filters to be more specific with our selections.

css=input[name='continue'][type='button']

Here Selenium will act on the input field with name=”continue” and type=”button”

Choosing a specific match

CSS selectors in Selenium allow us to navigate lists with more finess that the above methods. If we have a ul and we want to select its fourth li element without regard to any other elements, we should use nth-child or nth-of-type.

 

  • <p>Heading</p>       
  • Cat
           
  • Dog
           
  • Car
           
  • Goat
If we want to select the fourth li element (Goat) in this list, we can use the nth-of-type, which will find the fourth li in the list.

css=ul#recordlist li:nth-of-type(4)

On the other hand, if we want to get the fourth element only if it is a li element, we can use a filtered nth-child which will select (Car) in this case.

css=ul#recordlist li:nth-child(4)

Note, if you don’t specify a child type for nth-child it will allow you to select the fourth child without regard to type. This may be useful in testing css layout in selenium.

css=ul#recordlist *:nth-child(4)Sub-string matches

CSS in Selenium has an interesting feature of allowing partial string matches using ^=, $=, or *=. I’ll define them, then show an example of each:
^=Match a prefix
$=Match a suffix
*=Match a substring

css=a[id^='id_prefix_']

A link with an “id” that starts with the text “id_prefix_”

css=a[id$='_id_sufix']

A link with an “id” that ends with the text “_id_sufix”

css=a[id*='id_pattern']

A link with an “id” that contains the text “id_pattern”

Matching by inner text

And last, one of the more useful pseudo-classes, :contains() will match elements with the desired text block:

css=a:contains('Log Out')

This will find the log out button on your page no matter where it’s located. This is by far my favorite CSS selector and I find it greatly simplifies a lot of my test code.

转载于:https://www.cnblogs.com/ibelieveKelly/archive/2013/04/26/3045190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
XPath是一种XML文档的定位方法,也可以用于HTML文档的定位Selenium中也可以使用XPath来定位网页元素。下面是使用XPath定位元素的详细步骤: 1. 打开浏览器并访问网页: ```python from selenium import webdriver driver = webdriver.Chrome() driver.get("http://www.example.com") ``` 2. 使用XPath定位元素: ```python # 通过元素id定位 element = driver.find_element_by_xpath('//*[@id="element_id"]') # 通过元素name定位 element = driver.find_element_by_xpath('//*[@name="element_name"]') # 通过元素class定位 element = driver.find_element_by_xpath('//*[@class="element_class"]') # 通过元素标签名定位 element = driver.find_element_by_xpath('//tag_name') # 通过元素属性定位 element = driver.find_element_by_xpath('//*[@attribute_name="attribute_value"]') # 通过元素文本内容定位 element = driver.find_element_by_xpath('//*[text()="text_content"]') # 通过元素部分文本内容定位 element = driver.find_element_by_xpath('//*[contains(text(), "text_content")]') ``` 3. 对元素进行操作: ```python # 输入文本 element.send_keys("text_input") # 点击元素 element.click() # 获取元素文本 print(element.text) # 获取元素属性值 print(element.get_attribute("attribute_name")) ``` 注意事项: - XPath定位需要用到浏览器的开发者工具,在开发者工具中可以查看元素的XPath路径。 - XPath路径中的引号需要用不同类型的引号包裹,例如在单引号内使用双引号包裹。 - 如果XPath路径中包含斜杠(/),则需要使用双斜杠(//)或者使用单引号包裹整个XPath路径。 - 在XPath路径中没有找到元素时,会抛出NoSuchElementException异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值