selenium 进阶2: CSS SELECTOR 的完整版。

根据 最全面的官方文档: http://release.seleniumhq.org/selenium-core/1.0.1/reference.html

 

  • 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).

然后,我们找到其中提到的 "TestCssLocators" 这个文件:

 

http://code.google.com/p/selenium/source/browse/trunk/selenium/test/java/com/thoughtworks/selenium/corebased/TestCssLocators.java?r=10587

 

package com.thoughtworks.selenium.corebased;

import com.thoughtworks.selenium.InternalSelenseTestNgBase;

import org.testng.annotations.Test;

public class TestCssLocators extends InternalSelenseTestNgBase {
	@Test public void testCssLocators() throws Exception {

		//         Unimplemented features:
		//             namespace
		//             pseudo element
		//                 ::first-line
		//                 ::first-letter
		//                 ::selection
		//                 ::before
		//                 ::after
		//             pseudo class including:
		//                 :nth-of-type
		//                 :nth-last-of-type
		//                 :first-of-type
		//                 :last-of-type
		//                 :only-of-type
		//                 :visited
		//                 :hover
		//                 :active
		//                 :focus
		//                 :indeterminate
		//         

		selenium.open("../tests/html/test_locators.html");

		// css2 selector test

		// universal selector

		verifyTrue(selenium.isElementPresent("css=*"));

		// only element type

		verifyEquals(selenium.getText("css=p"), "this is the first element in the document");

		verifyEquals(selenium.getText("css=a"), "this is the first element");

		// id selector

		verifyEquals(selenium.getText("css=a#id3"), "this is the third element");

		// attribute selector

		verifyTrue(selenium.isElementPresent("css=input[name]"));

		verifyEquals(selenium.getText("css=a[href=\"#id3\"]"), "this is the third element");

		verifyFalse(selenium.isElementPresent("css=span[selenium:foo]"));

		verifyEquals(selenium.getText("css=a[class~=\"class2\"]"), "this is the fifth element");

		verifyEquals(selenium.getText("css=a[lang|=\"en\"]"), "this is the sixth element");

		// class selector

		verifyTrue(selenium.isElementPresent("css=a.a1"));

		// pseudo class selector

		verifyEquals(selenium.getText("css=th:first-child"), "theHeaderText");

		verifyEquals(selenium.getText("css=a:lang(en)"), "this is the first element");

		verifyEquals(selenium.getText("css=#linkPseudoTest :link"), "link pseudo test");

		// descendant combinator

		verifyEquals(selenium.getText("css=div#combinatorTest a"), "and grandson");

		// child combinator

		verifyEquals(selenium.getText("css=div#combinatorTest > span"), "this is a child and grandson");

		// preceding combinator

		verifyEquals(selenium.getText("css=span#firstChild + span"), "another child");

		// css3 selector test

		// attribuite test

		verifyEquals(selenium.getText("css=a[name^=\"foo\"]"), "foobar");

		verifyEquals(selenium.getText("css=a[name$=\"foo\"]"), "barfoo");

		verifyEquals(selenium.getText("css=a[name*=\"zoo\"]"), "foozoobar");

		verifyEquals(selenium.getText("css=a[name*=\"name\"][alt]"), "this is the second element");

		// pseudo class test

		verifyTrue(selenium.isElementPresent("css=html:root"));

		verifyEquals(selenium.getText("css=div#structuralPseudo :nth-child(2n)"), "span2");

		verifyEquals(selenium.getText("css=div#structuralPseudo :nth-child(2)"), "span2");

		verifyEquals(selenium.getText("css=div#structuralPseudo :nth-child(-n+6)"), "span1");

		verifyEquals(selenium.getText("css=div#structuralPseudo :nth-last-child(4n+1)"), "span4");

		verifyEquals(selenium.getText("css=div#structuralPseudo :nth-last-child(2)"), "div3");

		verifyEquals(selenium.getText("css=div#structuralPseudo :nth-last-child(-n+6)"), "span3");

		verifyEquals(selenium.getText("css=div#structuralPseudo :first-child"), "span1");

		verifyEquals(selenium.getText("css=div#structuralPseudo :last-child"), "div4");

		verifyEquals(selenium.getText("css=div#onlyChild span:only-child"), "only child");

		verifyTrue(selenium.isElementPresent("css=span:empty"));

		verifyEquals(selenium.getText("css=div#targetTest span:target"), "target");

		verifyTrue(selenium.isElementPresent("css=input[type=\"text\"]:enabled"));

		verifyTrue(selenium.isElementPresent("css=input[type=\"text\"]:disabled"));

		verifyTrue(selenium.isElementPresent("css=input[type=\"checkbox\"]:checked"));

		verifyEquals(selenium.getText("css=a:contains(\"zoo\")"), "foozoobar");

		verifyEquals(selenium.getText("css=div#structuralPseudo span:not(:first-child)"), "span2");

		verifyEquals(selenium.getText("css=div#structuralPseudo :not(span):not(:last-child)"), "div1");

		// combinator test

		verifyEquals(selenium.getText("css=div#combinatorTest span#firstChild ~ span"), "another child");
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值