我们如何在selenium webdriver中选择一个对象,每次点击提交按钮后id都会被更改。

时间:2016-02-15 10:26:13

标签: selenium-webdriver

使用for循环它首次选择对象但是在sumbit..it之后没有选择对象,因为提交后对象的id变化并显示错误信息..

代码段

   for(int i=4; i<10; i++)
         {
         driver.findElement(By.xpath("html/body/div[1]/aside[1]/section/ul[2]/li[5]/a/i[2]")).click();
         driver.findElement(By.xpath("html/body/div[1]/aside[1]/section/ul[2]/li[5]/ul/li[1]/a[1]/span")).click();
         driver.findElement(By.cssSelector(".mr5.btn.btn-primary.btn-sm.pull-right.ng-scope")).click();
         driver.findElement(By.xpath(".//*[@id='0' and @placeholder='Enter name']")).sendKeys("test"+i);
         driver.findElement(By.xpath(".//*[@id='0' and @placeholder='Enter partnumber']")).sendKeys("test"+i);

      driver.findElement(By.xpath(".//*[@id='product-description']")).sendKeys("test"+i);

      driver.findElement(By.xpath(".//*[@id='select2-chosen-4' and @class='select2-chosen']")).click();
      Select Group =new Select(driver.findElement(By.xpath(".//*[@ng-model='product.CategoryID']"))); 
      Group.selectByIndex(2);

下面的ID每次都在不断变化

   driver.findElement(By.xpath(".//*[@id='select2-chosen-4' and @class='select2-chosen']")).click();

我无法找到此元素独有的任何内容。

1 个答案:

答案 0 :(得分:0)

根据html,尝试使用className找到WebElement。

driver.findElement(By.className("select2-chosen")).click();