英文:
collect data for HashMap selenium java - what can be the xpath?
问题
以下是翻译好的内容:
@Test
public void visitSite() throws InterruptedException {
Thread.sleep(5000);
// 待办事项:验证位置标题是否显示 - 应该是“Locations”
WebElement loc = driver.findElement(By.xpath("//h2//span[@title='Locations']"));
String locTitle = loc.getText();
String expectedTitle = "Locations";
Assert.assertEquals(locTitle, expectedTitle);
// 待办事项:展开“更多+”位置标题的部分
WebElement expandMore = driver.findElement(By.xpath("//div[@data-metadata-id='locations']//div[@data-automation-id='wd-MoreLink']"));
expandMore.click();
Thread.sleep(4000);
// 待办事项:在 HashMap 中收集城市名称和工作数量
HashMap<String, String> regionJobsMap = new HashMap<String, String>();
// 下面的代码将在 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());
}
// 下面的代码将获取城市名称和工作数量,然后添加到 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 - 新泽西州 5
PA - 宾夕法尼亚州
注意:由于您要求只返回翻译的部分,我已经从原始文本中提取了相关的代码部分并进行了翻译。如有任何问题,请随时提问。
英文:
In the site this is how it looks City Name- Number of jobs
Site: https://amfam.wd1.myworkdayjobs.com/HomesiteCareers/4/refreshFacet/318c8bb6f553100021d223d9780d30be
I was thinking of putting the name and corresponding job number in a HashMap, however the following approach which gets the city name and then job bumbers 1st, will not work.
@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
Question: What would be a good way to Get the City name and corresponding job number? I cannot think of 1 xpath that will give me both. If I use separate xpaths then actual city and job numbers will not match. Thanks in advance for your time.
答案1
得分: 0
使用以下代码在单击“More”链接后将值存储在哈希映射中:
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']")); //获取位置列表
for (int i = 0; i < locations.size(); i++) //遍历列表
{
String cities = locations.get(i).findElement(By.cssSelector("label[id*=CheckBox-locations]")).getText(); //获取位置名称
String jobNumber = locations.get(i).findElement(By.cssSelector("span[id*=CountLabel-locations]")).getText().replaceAll("[^0-9]", ""); //获取特定位置的工作数
hm.put(cities, jobNumber); //将其作为键值对放入哈希映射中
}
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()); //获取值
}
注意:哈希映射不保持任何顺序。如果需要保持顺序,您可以使用LinkedHashMap或使用比较器来保持TreeMap排序。
英文:
Use the below code to store values in hashmap after you click on 'More' link:
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
}
Note: Hashmap doesn't maintain any order. If that is the case you can use LinkedHashMap or use comparator to keep TreeMap sorted.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论