您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
selenium實踐-用css去定位元素
作者:rital 發(fā)布時間:[ 2017/1/12 9:55:12 ] 推薦標(biāo)簽:功能測試 Selenium

  一直以來都用xpath去識別元素,xpath確識方便又實用,但有時會發(fā)現(xiàn)它要寫很長很長,對于有些元素,用css去定位,可能更加方便。
  假設(shè)有如下的HTML頁面:
  line 1 <html>
  line 2   <body>
  line 3     <form id= "loginForm" >
  line 4       <input name= "username" type= "text" />
  line 5       <input name= "password" type= "password" />
  line 6       <input name= "continue" type= "submit" value= "Login" />
  line 7       <a href= "continue.html" >Continue</a>
  line 8     </form>
  line 9   </body>
  line 10 <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是否正確。

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd