'Web Scraping-Selenium/Java
public class webscrapping
{private static final String CSV_FILE_NAME = null;
public static WebDriver driver;public static void main(String[] args) throws InterruptedException, IOException {DesiredCapabilities dc = new DesiredCapabilities();dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);WebDriverManager.chromedriver().setup();driver = new ChromeDriver();driver.get("https://www.indeed.com/");Scanner myObj = new Scanner(System.in);System.out.println("What jobs are you looking for ? ");String job_Search = myObj.nextLine();driver.findElement(By.id("text-input-what")).sendKeys(job_Search);driver.findElement(By.xpath("//*[@id='jobsearch']/button")).click();driver.manage().window().maximize();Thread.sleep(2000);
List<String > name = new ArrayList<String>();
for (int j = 1;j < 3; j++) {
WebElement jobCard = driver.findElement(By.xpath("//div[@id='mosaic-provider-jobcards']"));
List<WebElement> jobCardWitha = jobCard.findElements(By.tagName("a"));
Thread.sleep(1000);
WebElement companyname1=driver.findElement(By.xpath("//div[@class='heading6 company_location tapItem-gutter companyInfo']"));
for (WebElement webElement : jobCardWitha) {
Thread.sleep(1000);
String name1=webElement.getText();
name.add(name1);
Thread.sleep(1000);
FileWriter fr= new FileWriter("C:\\Users\\User\\eclipse-workspace\\ParametrizedTest\\src\\joblist.csv",true);
BufferedWriter br= new BufferedWriter(fr);
br.newLine();
for(int i=0;i<name.size();i++)
{
br.write(name.get(i)+","+name.get(i++));
br.newLine();
}
br.close();
} System.out.println(name);
WebElement pagei = driver.findElement(By.xpath("//a[@aria-label='Next']"));
pagei.click();
try{
// System.out.println("Waiting for Alert");
Thread.sleep(2000);
WebElement popup=driver.findElement(By.xpath("//*[@id='popover-x']/button"));
popup.click();
System.out.println("Alert Displayed");
}
catch (Exception e){
System.out.println("Alert not Displayed");
}
}
}
I am able to scrape the data from the indeed job portal as a job cards and able to save to csv file. but i need to separate the values as a company name and location as column names in csv and the locators for the company name is //div\[@ class="heading6 company_location tapItem-gutter companyInfo"\] whether i need to loop this inside the job card or separately. i'm confused how to extract the company name and location and it needs to be added to the csv.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
