如何根据Selenium提供的HTML定位输入标签?

时间:2018-09-06 11:18:19

标签: selenium selenium-webdriver xpath css-selectors webdriver

请为以下内容使用什么定位器。我尝试过Xpath和CSS Selector,但是没有运气。

<input type="password" 
       class="input-block-level ng-dirty ng-valid ng-valid-required" 
       placeholder="Password" ng-model="password" ng-trim="false"
       required="" ng-disabled="isLogging" 
       ng-hide="changePassword" autocomplete="off">

2 个答案:

答案 0 :(得分:0)

根据HTML,您可以使用以下任一解决方案:

  • CSS_SELECTOR

    "input.input-block-level.ng-dirty.ng-valid.ng-valid-required[ng-model='password']"  
    
  • XPATH

    "//input[@class='input-block-level ng-dirty ng-valid ng-valid-required' and @ng-model='password']"
    

注意:该元素是Angular元素,请确保您与引发 WebDriverwait 的元素进行交互。

答案 1 :(得分:0)

对于样式类ng-dirty ng-valid ng-valid-required,Angular编译源代码后,Angular编译器会自动插入它们。因此,您的定位器不应依赖这些样式类。

1) Using Java as script language

   driver.findElement(By.cssSelector("input[placeholder='Password']"))
   // or
   driver.findElement(By.xpath("//input[@placeholder='Password']"))

2) Using python as script language

   dirver.find_element_by_css_selector("input[placeholder='Password']")
   // or
   driver.find_element_by_xpath("//input[@placeholder='Password']")