2016-11-18 85 views
1

我需要在我的網站中自動鏈接。我想在循環的每次迭代中傳遞一次xpath值。所以,我可以減少我的編碼
公共類彈出 {如何爲selenium java代碼添加循環條件

private WebDriver driver; 
    private String baseUrl; 
    //private boolean acceptNextAlert = true; 
    private StringBuffer verificationErrors = new StringBuffer(); 

    @Before 
    public void setUp() throws Exception { 
    driver = new FirefoxDriver(); 
    baseUrl = "http://www.example.com/info.php"; 
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test 
    public void testPopup() throws Exception { 
    driver.findElement(By.xpath("//div[@id='info-list']/div[2]/div/div/div/div/a[2]/i")).click(); 
    driver.findElement(By.xpath("//div[@id='info-list']/div[2]/div/div/div/div/a[3]/i")).click(); 
    driver.findElement(By.xpath("//div[@id='info-list']/div[2]/div/div/div/div/a[4]/i")).click(); 
    driver.findElement(By.xpath("//div[@id='info-list']/div[2]/div/div/div/div/a[5]/i")).click(); 

回答

0

你能試試嗎?

for(int j=2; j<=5; j++) { 
    driver.findElement(By.xpath("//div[@id='info-list']/div[2]/div/div/div/div/a["+j+"]/i")).click(); 
} 
0

嗨Sanchit凱拉這種嘗試之一:

int i = 0; 

    //extract the link texts of each link element 
    for (WebElement e : linkElements) 
    { 
     linkTitles[i] = e.getText(); 
     i++; 
    } 

    //Test each link 
    for (String t : linkTitles) 
    {    
      // Titles Click 
      driver.findElement(By.linkText(t)).click();