Showing posts with label xpath. Show all posts
Showing posts with label xpath. Show all posts

Wednesday, June 18, 2014

Using not() in XPath. (ex- you want to write the xpath for the element whose class is not equal to 'abc').

Requirement-
In the below html code, we have to write the xpath for the span (high lighted in green color) tags present inside td tags where td class="closeTrigger" and tr class is not "week-selected".

HTML code-

<tr>
<td class="weekend closeTrigger">
<span>5</span></td>
<td class="closeTrigger">
<span>6</span></td>
<td class="closeTrigger">
<span>7</span></td>
<td class="closeTrigger">
<span>8</span></td>
<td class="closeTrigger">
<span>9</span></td>
<td class="closeTrigger">
<span>10</span></td>
<td class="weekend closeTrigger">
</tr>
<tr class="week-selected">
<td class="weekend closeTrigger date-selected">
<span>11</span></td>
<td class="closeTrigger">
<span>12</span></td>
<td class="closeTrigger">
<span>13</span></td>
<td class="closeTrigger todays-date">
<span>14</span></td>
<td class="closeTrigger">
<span>15</span></td>
<td class="closeTrigger">
<span>16</span></td>
<td class="weekend closeTrigger">
</tr>
<tr>
<td class="weekend closeTrigger">
<span>17</span></td>
<td class="closeTrigger">
<span>18</span></td>
<td class="closeTrigger">
<span>19</span></td>
<td class="closeTrigger">
<span>20</span></td>
<td class="closeTrigger">
<span>21</span></td>
<td class="closeTrigger">
<span>22</span></td>
<td class="weekend closeTrigger">
</tr>


Correct xpath- //tr[not(@class='week-selected')]//td[@class='closeTrigger']//span

For reference, here is the verified snapshot from firepath.




webpage-

Wednesday, May 7, 2014

How to save the images in a newly created folder while execution.

Note- create a folder with name Images and have InputData.xslx and just copy paste the below code and run it

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Kalai {
public static void main(String[] args) throws InvalidFormatException, IOException {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.urbanspoon.com/c/1/Seattle-restaurants.html");
driver.findElement(By.partialLinkText("City of Seattle")).click();
List<WebElement> nameOfRestros = driver.findElements(By.className("resto_name"));
int numOfRestros = nameOfRestros.size();
for(int i=0; i<numOfRestros; i++){
nameOfRestros.get(i).click();
mainProc(driver,i);
driver.get("http://www.urbanspoon.com/.../City-of-Seattle-restaurants");
nameOfRestros = driver.findElements(By.className("resto_name"));
}

driver.close();
}
public static void mainProc(WebDriver driver, int rowNum) throws InvalidFormatException, IOException{
String nameOfResto = driver.findElement(By.xpath("//h1[@itemprop='name']")).getText().trim();

boolean create = true;
writeData(rowNum, 0, nameOfResto, create);
create = false;
String phone = driver.findElement(By.xpath("//a[@class='phone tel no-link']")).getText().trim();

writeData(rowNum, 1, phone, create);
String address = driver.findElement(By.className("tags")).getText().trim();

writeData(rowNum, 2, address, create);

driver.findElement(By.linkText("View menu")).click();

List<WebElement> img = driver.findElements(By.xpath("//div[@class='list']//div[@class='image']//img"));

System.out.println(img.size());
for(int i=0; i<img.size(); i++){
String imgSrc = img.get(i).getAttribute("src");

WebElement dishEle = img.get(i).findElement(By.xpath("..")).findElement(By.xpath("..")).findElement(By.xpath("..")).findElement(By.xpath(".."));
String dishName = dishEle.findElement(By.tagName("h3")).getText();
File folder = new File("./Images/"+nameOfResto);
folder.mkdir();

String folderName = folder.getAbsolutePath();
pasteImg(folderName, imgSrc, dishName, i);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,10000)", "");
}
}
public static void writeData(int rowNum, int cellNum, String value, boolean create) throws InvalidFormatException, IOException{
FileInputStream fis = new FileInputStream("./InputData.xlsx");
Workbook wb = WorkbookFactory.create(fis);
if(create)
wb.getSheet("Sheet3").createRow(rowNum).createCell(cellNum).setCellValue(value);
else
wb.getSheet("Sheet3").getRow(rowNum).createCell(cellNum).setCellValue(value);
FileOutputStream fos = new FileOutputStream("./InputData.xlsx");
wb.write(fos);
}
public static void pasteImg(String folderName, String imgSrc, String dishName, int i) throws IOException{
URL url = new URL(imgSrc);
BufferedImage image = ImageIO.read(url);
File file = new File(folderName+"/"+dishName+".jpg");
ImageIO.write(image,"jpg", file);
}
}