1. 程式人生 > >Selenium webdriver常用命令彙總

Selenium webdriver常用命令彙總

這段時間使用selenium webdriver做UI自動化,積累了一些心得,彙總了常用的命令。
  1、查詢某個元素是否存在: bot.existElement(By.xpath("//a[contains(text(),'寧波測試商品002')]"))

  2、根據xpath斷言某個元素是否存在: Assert.assertEquals(bot.findElements(By.xpath("//div[@class='g-group-name']/span")).get(0).getText(),"H1237","合夥人小隊頁面錯誤!"); //index從0開始

  3、根據classname斷言某個元素是否存在: Assert.assertEquals(bot.findElements(By.className("col-name")).get(1).getText(),"趙利群","設定預設地址失敗!");
  4、嘗試多種方法定位元素:By.name()、By.id()、By.className()、By.linkText()、By.xpath()、By.tagName()、By.cssSelector()
  5、如果還是定位不到,可能是因為該元素存在iframe裡,需要先定位iframe再定位元素:
  bot.typeInFrame(By.id("yueForm"),By.xpath("//input[@id='OTPCtl2']"),wybpassword);
  bot.clickInFrame(By.id("yueForm"),By.id("bankCardSumbit"));
  6、type失敗時,可嘗試先使用click點選輸入框,然後使用type:
  bot.click(By.xpath("id('J_streetBox')/textarea"));//點選輸入框
  bot.type(By.name("street"), newaddress);//修改地址
  7、切換視窗:bot.changeWindow(1);//index從0開始
  8、比較跳轉後url是否正確:bot.compareUrl(1,"

http://www.kaola.com/cart.html?from=Repeat_purchase")
  9、有時用seleniumIDE可以實現replay操作,但是webdriver不行,增加等待時間或許可以解決問題:bot.wait(5);//0.1秒為單位