Kindly suggest in my code to make my xpath data driven. | Selenium Forum
M
Posted on 24/11/2015
Hi, Can I use string in xpath two times

Like String Marketid I have used in


driver.findElement(By.xpath("//*[@id='mainCol']/div/div[3]/div["+marketId+"]/div[1]/div[1]")).click();

driver.findElement(By.xpath("//*[@id='mainCol']/div/div[3]/div["+marketId+"]/div[2]/div/button["+buttonId+"]")).click();

beacuse I have one event under that same event i have button , I need to click on that button.

Actual xpaths are :
//*[@id='mainCol']/div/div[3]/[b:2n3qhu8t]div[12[/b:2n3qhu8t]]/div[1]/div[1] of my market
button is present below this market.
//*[@id='mainCol']/div/div[3]/[b:2n3qhu8t]div[12][/b:2n3qhu8t]/div[2]/div/button[1] // this is xpath of same market's button

So I want to make xpath customisable so that i can read the input from xls Like, market , button
here div[12] is the market this div is present in button's xpath too because each market has its own buttons

Kindly suggest



like in below code
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Inplay {

@Test(dataProvider = "getData")
public void Inplay(String eventId, String marketId, String buttonId) {


WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.amazon.com");
//driver.manage().window().maximize();

driver.findElement(By.xpath("//*[@id= "+eventId+"]/div[1]")).click();

driver.findElement(By.xpath("//*[@id='mainCol']/div/div[3]/div["+marketId+"]/div[1]/div[1]")).click();

driver.findElement(By.xpath("//*[@id='mainCol']/div/div[3]/div["+marketId+"]/div[2]/div/button["+buttonId+"]")).click();

M
Replied on 24/11/2015

whats the problem Code seems fine to me. just instead of strings you should use hash table(explained in data driven framework).