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
3 Answers 3
Edit: Updated code according to comment.
You need to execute following code.
driver.get("http://www.espn.in/mlb/schedule/_/date/20170329");
List<WebElement> list = driver.findElements(By.xpath("//*[@id='sched-container']/div[2]/table/tbody/tr"));
int rows = list.size();
for (int i= 1; i <= rows; i++) {
List<WebElement> links = driver.findElements(By.xpath("//*[@id='sched-container']/div[2]/table/tbody/tr["+i+"]//a"));
String rowLinks = "";
for (WebElement link : links) {
rowLinks = rowLinks + link.getAttribute("href") + " , ";
}
System.out.println(rowLinks);
}
-
It is getting all href links. Is it possible to get href links for particular div (div2) and row wise?Ravi– Ravi2017年03月27日 11:32:40 +00:00Commented Mar 27, 2017 at 11:32
-
@Ravi update code according to your comment please check.Jeevan Bhushetty– Jeevan Bhushetty2017年03月27日 11:49:17 +00:00Commented Mar 27, 2017 at 11:49
-
Welcome.. Happy to help.. Please mark this question as solved :)Jeevan Bhushetty– Jeevan Bhushetty2017年03月27日 12:29:11 +00:00Commented Mar 27, 2017 at 12:29
JSoup to parse webpage
I would like to suggest use of JSoup library if you intend to read the data from a web page. Starting a browser can take more time. Download JSoup JAR or use Maven.
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
class read_data {
public static void main(String[] args) {
try {
Document doc = Jsoup.connect("http://www.espn.in/mlb/schedule/_/date/20170329").get();
Elements links = doc.select("a[class=team-name]");
for (Element link : links) {
System.out.println(link.attr("abs:href"));
}
} catch (IOException ex) {
}
}
}
JSoup will save your time as compared to Selenium webdriver.
simply this should do the thing
d.get("http://www.espn.in/mlb/schedule/_/date/20170329");
System.out.println(d.findElements(By.xpath("//table[contains(@class,'schedule has-team')]//tbody//tr//td//a")).size());
d.findElements(By.xpath("//table[contains(@class,'schedule has-team')]//tbody//tr//td//a")).
forEach(ref -> {
System.out.println(ref.getAttribute("href"));
});
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']