2015-02-09 87 views
0

我的情景:如何搜索和數據輸入到搜索字段

  1. 創建ID
  2. 輸入ID進入搜索領域,這是在步驟1中創建

如果id在當前屏幕上可見/可用的id列表中(下面的屏幕截圖)可用,我可以獲取id並輸入搜索字段。但是,如果ID在當前屏幕上不可見/可用,則腳本無法檢索它。在這種情況下,我將不得不點擊右箭頭按鈕進入下一個列表,在那裏搜索該ID,然後輸入搜索字段。

我的代碼在下面的代碼中獲取id,如果它在第一個屏幕中可用的話。我曾嘗試使用if語句,但它不起作用。任何幫助是極大的讚賞!

我的腳本:

/*3. Enter the Site ID of the advertiser created in the previous step.*/ 

     //this finds the ANY <a> tag with the text supplied by the site_id 
     WebElement siteId = driver.findElement(By.xpath("//a[.='" + site_id + "']")); 
     WebDriverWait wait = new WebDriverWait(driver, 60); 
     wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[.='" + site_id + "']"))); 

     if(!(driver.findElement(By.xpath(PvtConstants.READ_SITES_IDS)).getText().contains("//a[.='" + site_id + "']"))){ 

//click on the Right-Arrow Button 
      driver.findElement(By.xpath(".//*[@id='dataTableSite_wrapper']/div[3]/div[2]/span[3]")).click(); 

      //get a text/copy of the site_id 
      String getThePreviouslyCreatedsiteId = siteId.getText().trim(); 

      //insert the text into the Advanced Filters Search Field 
      driver.findElement(By.id(PvtConstants.READ_SITES_ADVANCED_FILTER_SITE_SEARCH_FIELD)).sendKeys(getThePreviouslyCreatedsiteId); 

UI截圖: enter image description here

+1

你必須使用findElements知道Id是否存在比使用if條件。請觀看此視頻以獲取詳細說明。 https://www.youtube.com/watch?v=4xILWxRC9aA。如果你能提供你正在得到的錯誤,那將是非常好的。 – Uday 2015-02-09 04:14:59

+0

gettext將如何包含您傳遞的xpath?在屏幕上似乎沒有任何xpath可見 – 2015-02-09 06:33:24

回答

1

在這種情況下,你可以做一件事......

得到否定的。你得到的參賽作品(如27),並將其分爲no。你必須去的分頁。之後,在一個循環中(就像這裏它會是3),通過xpath使用給定的site_id找到元素,如果它可用,它將打破循環,否則點擊下一頁。代碼將有點像這樣:

WebDriverWait wait = new WebDriverWait(driver, 60); 
WebElement siteId = null; 
while(i!=pagination){ // pagination is no. of pages you need to scrap 
    try{ 
     siteId = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[.='" + site_id + "']"))); // will check for element to be visible, if not found then timeout exception will be thrown 
     break; // if found will break the loop 
    } catch(TimeoutException toe){ 
      driver.findElement(By.xpath(".//*[@id='dataTableSite_wrapper']/div[3]/div[2]/span[3]")).click(); // clicking next arrow 
      i++; // incrementing counter 
    } 
} 
+0

很不錯,Vivek !!!!謝啦! – familyGuy 2015-02-09 17:27:25

+0

樂於幫助哥們! – 2015-02-10 05:18:32