一直以来都用xpath去识别元素,xpath确识方便又实用,但有时会发现它要写很长很长,对于有些元素,用css去定位,可能更加方便。
假设有如下的HTML页面:
line 1<html>
line 2<body>
line 3<formid="loginForm">
line 4<inputname="username"type="text"/>
line 5<inputname="password"type="password"/>
line 6<inputname="continue"type="submit"value="Login"/>
line7<a href= "continue.html" >Continue</a>
line8</form>
line9</body>
line10<html>
通过XPath定位
例如:
driver.click("xpath=/html/body/form[1]")。如果使用相对路径,则可以省略"xpath=":
driver.click("//form[1]");
driver.click("//form[@id='loginForm']");
driver.click("//form[@id='loginForm']/input[1]");
driver.click("//input[@name=‘username’]");
通过css定位
例如:
driver.click("css=form#loginForm");
driver.click('css=input[name="username"]');
driver.click("css=#loginForm input:nth-child(2)");
怎么查看页面元素的各个属性呢?推荐使用Firefox的Firebug插件,可以自动生成指定元素的xpath和css。同时配合Firefinder,可以验证你的xpath和css是否正确。
分享到:
相关推荐
selenium-server-standalone-2.40.0.jar 、selenium-java-2.40.0-srcs.jar 、selenium-java-2.40.0.jar
selenium-java-2.44.0.jar selenium-java-2.44.0-srcs.jar selenium-server-standalone-2.44.0.jar
java+selenium实现web自动化测试需要用到的jar包。不过某些情况下Selenium-server-standalone包即可,Selenium-java没有使用也可以运行
1. Download the latest version of Selenium RC 2. Unzip the file save it C:\selenium-remote-control-1.0.3 3. command prompt>java -version –> Version should grater than 1.6.0_5 4. command prompt go to ...
Selenium 服务器(selenium-server-4.1.1.jar)
selenium-java-2.45.0.jar
最新版selenium-java,selenium-server-standalone-3.141.0.jar
selenium-server-standalone-4.0.0-alpha-2.zip
selenium-java-2.41.0,selenium-java-2.33 完整包下载
selenium-server-standalone-3.141.59.jar
selenium-java-2.47.1.zip,2015年8月更新
selenium-server-standalone-2.44.0, selenium最新服务器,
selenium3.9.0-server-standalone.jar文件,需要的赶紧下载了
selenium-java-3.141.59.zip
selenium-server-standalone-2.44.0,selenium-java-2.44.0,java-client-2.2.0三件套
可用于selenium自动化测试中的一个jar包,selenium-html-runner-3.5.0.jar
selenium-server-standalone-3.141.59.jar selenium-server-standalone-3.141.59.jar
selenium-api-4.0.0- alpha- 6.jar 文件下载