I want to take the data and organize it without the tags. It looks something like this
Optical Zoom: | 15x |
Digital Zoom: | 6x |
Battery Type: | Alkaline |
Resolution Megapixels: | 14 MP |
and I want to be able to extract all the strings of information so that I can store in a plaintext file with just this:
Optical Zoom: 15x Digital Zoom: 6x Battery Type: Alkaline Resolution
Megapixels: 14 MP
public static void main(String[] args) {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override", "some UA string");
WebDriver driver = new FirefoxDriver(profile);
String Url = "http://www.walmart.com/ip/Generic-14-MP-X400-BK/19863348";
driver.get(Url);
List resultsDiv = driver.findElements(By.xpath("//table[contains (@class,'SpecTable')//td"));
System.out.println(resultsDiv.size());
for (int i=0; i
System.out.println(i+1 + ". " + resultsDiv.get(i).getText());
}
I am programming in Java with Selenium and I cannot figure out the correct XPath expression for it.
Can someone figure out why I err on this and maybe give me some pointers on how I can parse this data correctly? Im very new to Selenium and XPaths but I need this for work.
Also if anyone has any good sources for me to learn Selenium and XPath fast, those would also be greatly appreciated!
解决方案
Probably this will suite your needs:
string text = driver.findElement(By.cssSelector("table.SpecTable")).getText();
String text will contain all text nodes from the table with class SpecTable.
I prefer using css, because it's supported by IE and faster than xpath. But as for xpath tutorials try this and this.