Below is a working script for automating a little bit of Ebay's website. I'm very new to coding so this is definitely a messy code job. Which is why I would like to know how this code can be optimized using Java streams? I would also like to know if there is a easier way to write the FilterCarrier method in my code? Basically are there better ways to iterate? Thanks a lot!
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
public class EbayTestCase {
public static void main(String[] args) throws InterruptedException, IOException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Mehdee\\Documents\\Eclipse projects\\Selenium\\ChromeDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// WebDriverWait w = new WebDriverWait(driver, Duration.ofSeconds(30));
driver.get("https://www.ebay.com/");
String[] carrFilter = { "Verizon", "T-Mobile", "Unlocked", "1&1" };
driver.findElement(By.id("gh-ac")).sendKeys("iPhone 14");
driver.findElement(By.id("gh-btn")).click();
System.out.println(ResultCount(driver) + " For iPhone 14");
Thread.sleep(1500);
driver.findElement(By.cssSelector("input[aria-label='Apple iPhone 14 Pro Max']")).click();
System.out.println(ResultCount(driver) + " For Apple iPhone 14 Pro Max");
FilterCarrier(driver, carrFilter);
ElementScreenshot(driver);
driver.quit();
}
public static void ElementScreenshot(WebDriver driver) throws IOException {
WebElement ele = driver.findElement(
By.xpath("//body/div[5]/div[4]/div[2]/div[1]/div[2]/ul[1]/li[2]/div[1]/div[1]/div[1]/a[1]/div[1]"));
File SC = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Point location = ele.getLocation();
int height = ele.getSize().getHeight();
int width = ele.getSize().getWidth();
BufferedImage img = ImageIO.read(SC);
BufferedImage cropped = img.getSubimage(location.getX(), location.getY(), width, height);
ImageIO.write(cropped, "png", SC);
File fis = new File("C:\\Users\\Mehdee\\Documents\\SeleniumScreenshots\\screenshot.png");
FileHandler.copy(SC, fis);
System.out.println("Screenshot has been successfully stored in C://Documents/SeleniumScreenshots directory");
}
public static String ResultCount(WebDriver driver) {
String[] t = driver.findElement(By.cssSelector(".srp-controls__count-heading")).getText().split(" ");
String resultCount = t[0] + " Results Found";
return resultCount;
}
public static void FilterCarrier(WebDriver driver, String[] carrFilter) {
List<WebElement> options = driver
.findElements(By.xpath("//div[@id='x-refine__group_1__1']/ul/li/div/a/div/span/input"));
int b = 0;
for (int i = 0; i < options.size(); i++) {
List<String> carrNames = Arrays.asList(carrFilter);
WebElement option = options.get(i);
String label = option.getAttribute("aria-label");
if (carrNames.contains(label)) {
b++;
options.get(i).click();
System.out.println(ResultCount(driver) + " for: " + label);
options = driver.findElements(By.xpath("//div[@id='x-refine__group_1__1']/ul/li/div/a/div/span/input"));
if (b == 4) {
break;
}
}
}
}
}