Selenium - 使用xpath或cssSelector查找元素

时间:2017-10-20 11:55:07

标签: java selenium

我需要点击或查找元素" Compute vmSwitch"。我尝试了很多方法使用xpath(class& contains),cssSelector,但无法找到元素:

driver.findElement(By.xpath("//span[contains(@class,'nopadding vm-create-text-style-3 block-with-text-4  ng-binding') and contains(text(), 'Compute vmSwitch')]")).click();

代码如下:

<div class="w-full"><br>
<img class="img-responsive center-block m-t-47" src="/src/icon/background/create_vm_img5.png">
<div class="col-md-12 m-t-md  wordwrap">
<p class="nopadding vm-create-text-style-3 block-with-text-4  ng-binding">
Compute vmSwitch</p>
</div>

1 个答案:

答案 0 :(得分:1)

为什么尝试使用span标签? 如果这是你的html:

<html>
 <head></head>
 <body>
  <div class="w-full">
   <br> 
   <img class="img-responsive center-block m-t-47" src="/src/icon/background/create_vm_img5.png"> 
   <div class="col-md-12 m-t-md wordwrap"> 
    <p class="nopadding vm-create-text-style-3 block-with-text-4 ng-binding"> Compute vmSwitch</p> 
   </div> 
  </div>
 </body>
</html>
你可以尝试:

    WebElement elem2= driver.findElement(By.xpath("//div[@class='w-full']"));
    elem2.findElement(By.xpath(".//p[text()=' Compute vmSwitch']")).click();