gpt4 book ai didi

java - 为 HashMap selenium java 收集数据 - xpath 是什么?

转载 作者:行者123 更新时间:2023-12-01 16:55:08 25 4
gpt4 key购买 nike

在网站中,这就是它的外观城市名称 - 职位数量 enter image description here

网站:https://amfam.wd1.myworkdayjobs.com/HomesiteCareers/4/refreshFacet/318c8bb6f553100021d223d9780d30be

我正在考虑将姓名和相应的职位编号放入 HashMap 中,但是以下先获取城市名称,然后获取职位编号的方法将行不通。

 @Test
public void visitSite() throws InterruptedException {
Thread.sleep(5000);
//ToDo: Verify Locations Heading is displayed- should be Locations
WebElement loc = driver.findElement(By.xpath("//h2//span[@title='Locations']"));
String locTitle = loc.getText();
String expectedTitle = "Locations";
Assert.assertEquals(locTitle, expectedTitle);

//ToDo: Expand the More+ section of Locations title
WebElement expandMore = driver.findElement(By.xpath("//div[@data-metadata-id='locations']//div[@data-automation-id='wd-MoreLink']"));
expandMore.click();
Thread.sleep(4000);

//ToDo: Collect City name and Number of jobs in HashMap
HashMap<String, String> regionJobsMap = new HashMap<String, String>();

//This code below will collect City Names in an ArrayList
ArrayList<String> cities = new ArrayList<String>();
List<WebElement> citiesL = driver.findElements(By.xpath("//div[@data-metadata-id='locations']//div//label"));
for (WebElement ele : citiesL) {
cities.add(ele.getText());
}

//This code below will get City amd Job numbers and add to HashMap
ArrayList<String> jobNumbers = new ArrayList<String>();
List<WebElement> jobsL = driver.findElements(By.xpath("//div[@data-metadata-id='locations']//span"));
for (WebElement ele : jobsL) {
jobNumbers.add(ele.getText().replaceAll("[^0-9]", ""));
}
for (int i = 0, j = 0; i < cities.size() && j < jobNumbers.size(); i++, j++) {
regionJobsMap.put(cities.get(i), jobNumbers.get(j));
}

for(Map.Entry<String,String> mm:regionJobsMap.entrySet()){
System.out.println(mm.getKey() +" " + mm.getValue());
}
}

OUTPUT:
NJ - New Jersey 5
PA - Pennsylvania

问题:获取城市名称和相应职位编号的好方法是什么?我想不出 1 个 xpath 可以给我两者。如果我使用单独的 xpath,则实际城市和职位编号将不匹配。预先感谢您的宝贵时间。

最佳答案

单击“更多”链接后,使用以下代码将值存储在 HashMap 中:

    HashMap<String,String> hm= new HashMap<String,String>();
List<WebElement> locations = driver.findElements(By.xpath("//div[@data-metadata-id='locations']//div[@data-automation-id='facetValue']")); //get list of locations
for(int i=0; i<locations.size();i++) //iterate over list
{
String cities = locations.get(i).findElement(By.cssSelector("label[id*=CheckBox-locations]")).getText(); //get location name
String jobNumber = locations.get(i).findElement(By.cssSelector("span[id*=CountLabel-locations]")).getText().replaceAll("[^0-9]", ""); // get no. of jobs in that particular location
hm.put(cities, jobNumber); // put it as key-value pair in hashmap

}
Set s=hm.entrySet();
Iterator is=s.iterator();
while(is.hasNext())
{
Map.Entry m= (Map.Entry)is.next();
System.out.println(m.getKey()+" => "+m.getValue()); //get values
}

注意:Hashmap 不维护任何顺序。如果是这种情况,您可以使用 LinkedHashMap 或使用比较器来保持 TreeMap 排序。

关于java - 为 HashMap selenium java 收集数据 - xpath 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61602546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com