Java,Selenium:在按钮点击WebElement

时间:2017-05-16 13:37:56

标签: java selenium selenium-webdriver automated-tests

在我自动化的网页中,我想根据同一行中下一个单元格的文本值,选择表格单元格所包含的checkboxes

但是,每次点击一个复选框都会重新加载页面,以便在选中第一个复选框后获得 StaleElementException

ArrayList<WebElement> rows = (ArrayList<WebElement>)driver.findElements(By.xpath("html/body/form/table/tbody/tr[contains(@class, 'configsubcell_plain')]/td[contains(@class, 'configsubcell_plain')]/table/tbody/tr"));
for (WebElement row: rows) {    
    if(row.getText().contains("Accept"))
    {           
        ArrayList<WebElement> cells =(ArrayList<WebElement>)row.findElements(By.xpath("td"));           
        int i=1;
        for (WebElement cell: cells) 
        {  
          if(i==11)//the 12 the cell contains the value "Accept"
           {
             cell.click();              //wait.until(ExpectedConditions.presenceOfElementLocated(By.name("xmlCustomizationInput")));
        //rows = (ArrayList<WebElement>)driver.findElements(By.xpath("html/body/form/table/tbody/tr[contains(@class, 'configsubcell_plain')]/td[contains(@class, 'configsubcell_plain')]/table/tbody/tr"));
          }
          i++;          
        }
    }

网页设计有许多嵌套表格,因此我必须使用两个数组列表来识别我的产品。

点击后,我尝试使用wait条件并重新初始化行,但这也导致了相同的 StaleElementException

请在每次点击后重新加载页面后,帮助我检查所有checkboxes的值接受

1 个答案:

答案 0 :(得分:0)

我通过迭代整个页面找到包含“Accept”文本的行并将数据存储在数组中来解决问题

int i=1,j=0,rownum=0;

if(row.getText().contains("Accept"))
{           
    ArrayList<WebElement> cells =(ArrayList<WebElement>)row.findElements(By.xpath("td"));           

    for (WebElement cell: cells) 
    {  
      if(i==11)//the 12 the cell contains the value "Accept"
       {
         row_cell[j]=rownum;                
            j++;           
      }

    }
}

然后我迭代直接给出存储行号的xpath,单元格号总是11,所以我硬编码了这个值。要使循环等待,直到选中复选框,我将其保持在while循环中。检查后页面将被重新加载,异常发生,我让驱动程序等待并重新分配webelement并给予继续

for(int l=0; l<row_cell.length; l++) {
if(row_cell[l]!=0)
{

 apath="html/body/form/table/tbody/tr[contains(@class, 'configsubcell_plain')]/td[contains(@class, 'configsubcell_plain')]/table/tbody/tr["+row_cell[l]+"]/td[11]";
 System.out.println("path is: "+apath);
 WebElement rows1;
 try
 {
rows1 = driver.findElement(By.xpath(apath));`rows1.click();
while(!rows1.isSelected())`{
    System.out.println("in while");`}
 }
 catch(org.openqa.selenium.StaleElementReferenceException ex)
 {
     System.out.println("inside catch");
     WebDriverWait wait1 = new WebDriverWait(driver, 6);
       wait1.until(ExpectedConditions.presenceOfElementLocated(By.name("xmlCustut")));
      rows1 = driver.findElement(By.xpath(apath));
      continue;
 } `

`