2013-11-21 136 views
0

嗨,這裏的場景是我想在頁面的文本字段中輸入一些值,在此之前我需要使用xpath來查找文本字段。Selenium - 如何從Excel表格中獲取數據並將其寫入網頁?

所以,我在這裏有excel工作表中這些字段的值和xpaths。我如何獲得上面的代碼工作?這兩個片段都不起作用或顯示任何錯誤。

Cell cells = null; 
      Cell idcell=null; 
      if (checking.equalsIgnoreCase("xxxxxxxxxxxxx")){ 
       System.out.println("Checking xxxxxxxxxxxx"); 

       if (checking.equalsIgnoreCase("yyyyyyyyyyyy")){ 
       for (int cols = 0; cols < sheet.getColumns(); cols++) { 
        cells = sheet.getCell(cols, rows); 
        idcell=sheet.getCell(cols,rows); 
WebElement element_BuyCategory = driver.findElement(By.xpath(idcell.getContents())); 
    element_BuyCategory.sendKeys(cells.getContents()); 
     System.out.println("xpath ok"); 
        } 

迴應將不勝感激。

+0

請發佈[SSCCE](http://www.sscce.org)。沒有任何錯誤信息或能夠重現問題,沒有人能夠幫助你。 –

回答

0

我想你知道如何從你在上面的代碼中發佈的excel中提取數據。

對於使用XPath在頁面中查找文本框的部分,如果您使用CSS-Selectors而不是XPath,我認爲更容易,因爲XPath對於這些字段中的新手來說有點難以改變。

如果我們出現在StackOverflow上的頁面的右上角執行檢索例的文本框,在頁面的HTML代碼的下面:

<input class="textbox" type="text" value="" size="28" maxlength="240" tabindex="1" placeholder="search" name="q" autocomplete="off"></input> 

我的建議如果由於某種情況下要使用XPath的,你需要替換的行發現與NE元素

Cell cells = null; 
Cell idcell=null; 

if (checking.equalsIgnoreCase("xxxxxxxxxxxxx")) 
{ 
    System.out.println("Checking xxxxxxxxxxxx"); 

    if (checking.equalsIgnoreCase("yyyyyyyyyyyy")){ 
     for (int cols = 0; cols < sheet.getColumns(); cols++) 
     { 
      cells = sheet.getCell(cols, rows); 
      idcell = sheet.getCell(cols,rows);    
      WebElement element_BuyCategory = driver.findElement(
              By.cssSelector(
              "#search div input.textbox "); 

      /* Other way is find the element by its name, if its have a name */ 
      WebElement element_BuyCategory_by_Name = driver.findElement(
              By.name("q"); 

      element_BuyCategory.sendKeys(cells.getContents()); 
      System.out.println("xpath ok"); 
     } 
} 

:是使用下面的代碼XT行:

/* If it's the only text box with this class or it's the first, I repeat this is a example */ 
WebElement element_BuyCategory = driver.findElement 
           By.xpath("//input[contains(@class,'textbox')")); 

我希望上面的例子解釋可以幫助你。

+0

嗨Vkt0r,感謝您的迴應,它真的幫助完整。但這裏的重點是我想要循環語句來確定在Excel表中有多少xpaths,然後我想讓Web瀏覽器找到Web元素,然後在下一行中,我應該輸入相應的值到該Web元素也出現在Excel表格中。我知道這將是有點混亂,請嘗試,如果可能的話。我認爲沒有人會嘗試這種方式..它減少了大量的編碼。 –

+0

我不明白你想要什麼。看看你是否遇到XPath到CSS選擇器的翻譯問題我推薦你在[link](https://github.com/santiycr/cssify)中設置一個簡單的翻譯器,這真的非常有用 –

相關問題