Here is the website link (http://www.espn.in/mlb/schedule/_/date/20170329) Below is my code. I need to get all href hyperlinks. When i executed the below code its showing as null.
Output should be
http://www.espn.in/mlb/team/_/name/tex/texas-rangers http://www.espn.in/mlb/team/_/name/col/colorado-rockies http://www.espn.com/mlb/conversation?gameId=370328127
http://www.espn.in/mlb/team/_/name/chw/chicago-white-sox http://www.espn.in/mlb/team/_/name/kc/kansas-city-royals http://www.espn.com/mlb/conversation?gameId=370328107
http://www.espn.in/mlb/team/_/name/cle/cleveland-indians http://www.espn.in/mlb/team/_/name/mil/milwaukee-brewers http://www.espn.com/mlb/conversation?gameId=370328108
http://www.espn.in/mlb/team/_/name/sf/san-francisco-giants http://www.espn.in/mlb/team/_/name/chc/chicago-cubs http://www.espn.com/mlb/conversation?gameId=370328116
http://www.espn.in/mlb/team/_/name/lad/los-angeles-dodgers http://www.espn.in/mlb/team/_/name/cin/cincinnati-reds http://www.espn.com/mlb/conversation?gameId=370328117
http://www.espn.in/mlb/team/_/name/sd/san-diego-padres http://www.espn.in/mlb/team/_/name/lad/los-angeles-dodgers http://www.espn.com/mlb/conversation?gameId=370328119
http://www.espn.in/mlb/team/_/name/oak/oakland-athletics http://www.espn.in/mlb/team/_/name/laa/los-angeles-angels http://www.espn.com/mlb/conversation?gameId=370328103
http://www.espn.in/mlb/team/_/name/ari/arizona-diamondbacks http://www.espn.in/mlb/team/_/name/sea/seattle-mariners http://www.espn.com/mlb/conversation?gameId=370328112
http://www.espn.in/mlb/team/_/name/bos/boston-red-sox http://www.espn.in/mlb/team/_/name/pit/pittsburgh-pirates http://www.espn.com/mlb/conversation?gameId=370328123
http://www.espn.in/mlb/team/_/name/det/detroit-tigers http://www.espn.in/mlb/team/_/name/nyy/new-york-yankees http://www.espn.com/mlb/conversation?gameId=370328110
http://www.espn.in/mlb/team/_/name/mia/miami-marlins http://www.espn.in/mlb/team/_/name/wsh/washington-nationals http://www.espn.com/mlb/conversation?gameId=370328120
Code
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "D:\\D Drive\\Software\\Selenium Latest Software\\geckodriver.exe");
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// maximize window
driver.manage().window().maximize();
// pass url
driver.get("http://www.espn.in/mlb/schedule/_/date/20170329");
//Get number of rows In table.
int Row_count = driver.findElements(By.xpath(".//*[@id='sched-container']/div[2]/table/tbody/tr")).size();
System.out.println("Number Of Rows = "+Row_count);
//Get number of columns In table.
int Col_count = driver.findElements(By.xpath(".//*[@id='sched-container']/div[2]/table/tbody/tr[1]/td")).size();
System.out.println("Number Of Columns = "+Col_count);
//divided xpath In three parts to pass Row_count and Col_count values.
String first_part = ".//*[@id='sched-container']/div[2]/table/tbody/tr[";
String second_part = "]/td[";
String third_part = "]";
for(int i=1; i<=Row_count; i++) {
for(int j=1; j<=Col_count; j++ )
{
String final_xpath = first_part+i+second_part+j+third_part;
//String Table_data = driver.findElement(By.xpath(final_xpath)).getText();
String Table_data = driver.findElement(By.xpath(final_xpath)).getAttribute("href");
System.out.print(Table_data +" ");
}
System.out.println("");
}
//Wait for 5 Sec
Thread.sleep(5);
// Close the driver
driver.quit();
}
}
Actual output:
OUTPUT:
Number Of Rows = 20
Number Of Columns = 7
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
href
you need to point to a link, in your current case you don't, you need a selector to end ina
like .//*[@id='sched-container']/div[2]/table/tbody/tr/td/a[@class='team-name']