日期:2014-05-17  浏览次数:20641 次

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.isElemen