【发布时间】:2020-05-04 22:16:21
【问题描述】:
网站: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,那么实际的城市和工作编号将不匹配。提前感谢您的宝贵时间。
【问题讨论】:
-
这个怎么样://div[@data-metadata-id='locations']//span[starts-with(text(),'(') and substring(text(),字符串长度(文本())-字符串长度(')')+1)=')']