2016-09-21 135 views
1

我一直在努力使登記過程在網站上,我有用戶以下腳本,硒的webdriver自動完成文本框

package agent; 

    import java.util.regex.Pattern; 
    import java.awt.List; 
    import java.util.concurrent.TimeUnit; 
    import org.junit.*; 
    import static org.junit.Assert.*; 
    import static org.hamcrest.CoreMatchers.*; 
    import org.openqa.selenium.*; 
    import org.openqa.selenium.chrome.ChromeDriver; 
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.openqa.selenium.support.ui.ExpectedConditions; 
    import org.openqa.selenium.support.ui.FluentWait; 
    import org.openqa.selenium.support.ui.Select; 
    import org.openqa.selenium.support.ui.WebDriverWait; 

    public class TaxRegistration { 
     private static final TaxRegistration Driver = null; 
    private WebDriver driver; 
    WebDriverWait wait; 
     private String baseUrl; 
     private boolean acceptNextAlert = true; 
     private StringBuffer verificationErrors = new StringBuffer(); 
     private By tagText = By.id("AddressDetails_City"); 


     @Before 
     public void setUp() throws Exception { 
     System.setProperty("webdriver.chrome.driver","E:/Vignesh/Automation/New folder (2)/chromedriver.exe"); 
     driver = new ChromeDriver(); 
     driver.manage().window().maximize(); 
     wait = new WebDriverWait(driver, 5); 
     baseUrl = "http://govreports.com.au/dev/taxagentt1.html"; 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     } 

     @Test 
     public void testBASFormWebdriver() throws Exception { 
      driver.get("http://govreports.com.au/dev/taxagentt1.html"); 
      driver.findElement(By.id("BAS")).click(); 
      Thread.sleep(1000); 
      driver.findElement(By.id("TFND")).click(); 
      Thread.sleep(1000); 
      driver.findElement(By.id("PAYG")).click(); 
      Thread.sleep(1000); 
      driver.findElement(By.id("TPAR")).click(); 
      Thread.sleep(1000); 
      //((JavascriptExecutor)driver).executeScript("scroll(0,2000)"); 
      Thread.sleep(1000); 
      driver.findElement(By.id("OUT")).click(); 
      Thread.sleep(1000); 
      //((JavascriptExecutor)driver).executeScript("scroll(0,1000)"); 
      Thread.sleep(1000); 
      driver.findElement(By.id("BUL")).click(); 
      Thread.sleep(1000); 
      driver.findElement(By.id("NUL")).click(); 
      Thread.sleep(1000); 
      driver.findElement(By.id("IUL")).click(); 
      Thread.sleep(1000); 
      //((JavascriptExecutor)driver).executeScript("scroll(0,6000)"); 
      Thread.sleep(1000); 
      driver.findElement(By.cssSelector("a.btn.btn-default > b")).click(); 
      Thread.sleep(2000); 
      driver.findElement(By.id("AgentNo")).clear(); 
      driver.findElement(By.id("AgentNo")).sendKeys("78301003"); 
      driver.findElement(By.id("AgentName")).clear(); 
      driver.findElement(By.id("AgentName")).sendKeys("Vignesh Check"); 
      driver.findElement(By.id("SendQuote")).click(); 
      Thread.sleep(2000); 
      driver.findElement(By.xpath("(//input[@name='plancodeTPAR'])[2]")).click(); 
      driver.findElement(By.xpath("(//input[@value='1'])[9]")).clear(); 
      driver.findElement(By.xpath("(//input[@value='1'])[9]")).sendKeys("2"); 
      driver.findElement(By.id("Proceed")).click(); 
      Thread.sleep(5000); 
      driver.findElement(By.id("ABN")).click(); 
      driver.findElement(By.id("ABN")).clear(); 
      driver.findElement(By.id("ABN")).sendKeys("19087046080"); 
      driver.findElement(By.id("BusinessName")).click(); 
      Thread.sleep(2000); 
      driver.findElement(By.id("Username")).clear(); 
      driver.findElement(By.id("Username")).sendKeys("[email protected]"); 
      driver.findElement(By.id("Password")).clear(); 
      driver.findElement(By.id("Password")).sendKeys("Viki2607"); 
      driver.findElement(By.id("ConfirmPassword")).clear(); 
      driver.findElement(By.id("ConfirmPassword")).sendKeys("Viki2607"); 
      String capColorDropDown = "//span[@role='listbox']"; 
      driver.findElement(By.xpath(capColorDropDown)).click(); 
      String itemName = "Orange"; 
      String listId = "color_listbox"; 
      Thread.sleep(2000); 
      String xpathForItem = "//ul[@id='Salutation_listbox']/li[@class='k-item' and text()='Mr']"; 
      driver.findElement(By.xpath(xpathForItem)).click(); 
      driver.findElement(By.id("FirstName")).click(); 
      driver.findElement(By.id("FirstName")).clear(); 
      driver.findElement(By.id("FirstName")).sendKeys("Vignesh"); 
      driver.findElement(By.id("LastName")).click(); 
      driver.findElement(By.id("LastName")).clear(); 
      driver.findElement(By.id("LastName")).sendKeys("Ks"); 
      driver.findElement(By.id("TelephoneAreaCode")).click(); 
      driver.findElement(By.id("TelephoneAreaCode")).clear(); 
      driver.findElement(By.id("TelephoneAreaCode")).sendKeys("02"); 
      driver.findElement(By.id("TelephoneNumber")).click(); 
      driver.findElement(By.id("TelephoneNumber")).clear(); 
      driver.findElement(By.id("TelephoneNumber")).sendKeys("2356895623"); 
      driver.findElement(By.id("AddressDetails_Line1")).clear(); 
      driver.findElement(By.id("AddressDetails_Line1")).sendKeys("Walker Street"); 

//我從下拉值選擇城市(自動完成文本框)我用下面這行,但它不是爲我工作

**`

driver.findElement(By.id("AddressDetails_City")).click();` 
    wait.until(ExpectedConditions.presenceOfElementLocated(tagText)); 
       WebElement textBoxElement = driver.findElement(tagText); 
       textBoxElement.sendKeys("sydn"); 
       selectOptionWithText("SYDNEY, NSW, Australia, 1001"); 
       //selectOptionWithIndex(0);** 
       Thread.sleep(2000); 
       driver.findElement(By.linkText("Next")).click(); 
       Thread.sleep(2000); 
    } 


     private void selectOptionWithIndex(int i) { 
     // TODO Auto-generated method stub 

    } 

    private void selectOptionWithText(String string) { 
     // TODO Auto-generated method stub 

    } 

    @After 
     public void tearDown() throws Exception { 
     driver.quit(); 
     String verificationErrorString = verificationErrors.toString(); 
     if (!"".equals(verificationErrorString)) { 
      fail(verificationErrorString); 
     } 
     } 

     private boolean isElementPresent(By by) { 
     try { 
      driver.findElement(by); 
      return true; 
     } catch (NoSuchElementException e) { 
      return false; 
     } 
     } 

     private boolean isAlertPresent() { 
     try { 
      driver.switchTo().alert(); 
      return true; 
     } catch (NoAlertPresentException e) { 
      return false; 
     } 
     } 

     private String closeAlertAndGetItsText() { 
     try { 
      Alert alert = driver.switchTo().alert(); 
      String alertText = alert.getText(); 
      if (acceptNextAlert) { 
      alert.accept(); 
      } else { 
      alert.dismiss(); 
      } 
      return alertText; 
     } finally { 
      acceptNextAlert = true; 
     } 
     } 
    } 

我不能能填補城市,州,郵政編碼和國家字段地址部分。我無法訪問這些字段中的自動完成值。我在腳本中用**符號突出顯示了問題區域。

我認爲該網站是可公開訪問的。有人可以幫我解決問題嗎?

回答

0

你可以單獨評論這個網址嗎?因爲你在腳本中提到的網址是說'因爲不活動而註銷'。

+0

現在你可以嘗試更新腳本,我已經改變了腳本中的baseUrl作爲http://govreports.com.au/dev/taxagentt1.html – user3836485

+0

我剛剛走過你的腳本,可否請你告訴我你的腳本所期望的確切的事情,因爲沒有評論和所有已經給出我無法得到預期的結果。 –

+0

我將腳本分成兩部分,我需要從自動完成下拉列表中選擇城市字段。我已經劃分並顯示了我使用特定評論的確切位置。但它不適合我。請提出一些解決方案,讓城市領域不受影響。 – user3836485