收集数据用于HashMap的Selenium Java – XPath可以是什么?

huangapple go评论71阅读模式
英文:

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
收集数据用于HashMap的Selenium Java – XPath可以是什么?

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(&quot;//h2//span[@title=&#39;Locations&#39;]&quot;));
String locTitle = loc.getText();
String expectedTitle = &quot;Locations&quot;;
Assert.assertEquals(locTitle, expectedTitle);
//ToDo: Expand the More+ section of Locations title
WebElement expandMore = driver.findElement(By.xpath(&quot;//div[@data-metadata-id=&#39;locations&#39;]//div[@data-automation-id=&#39;wd-MoreLink&#39;]&quot;));
expandMore.click();
Thread.sleep(4000);
//ToDo: Collect City name and Number of jobs in HashMap
HashMap&lt;String, String&gt; regionJobsMap = new HashMap&lt;String, String&gt;();
//This code below will collect City Names in an ArrayList
ArrayList&lt;String&gt; cities = new ArrayList&lt;String&gt;();
List&lt;WebElement&gt; citiesL = driver.findElements(By.xpath(&quot;//div[@data-metadata-id=&#39;locations&#39;]//div//label&quot;));
for (WebElement ele : citiesL) {
cities.add(ele.getText());
}
//This code below will get City amd Job numbers and add to HashMap
ArrayList&lt;String&gt; jobNumbers = new ArrayList&lt;String&gt;();
List&lt;WebElement&gt; jobsL = driver.findElements(By.xpath(&quot;//div[@data-metadata-id=&#39;locations&#39;]//span&quot;));
for (WebElement ele : jobsL) {
jobNumbers.add(ele.getText().replaceAll(&quot;[^0-9]&quot;, &quot;&quot;));
}
for (int i = 0, j = 0; i &lt; cities.size() &amp;&amp; j &lt; jobNumbers.size(); i++, j++) {
regionJobsMap.put(cities.get(i), jobNumbers.get(j));
}
for(Map.Entry&lt;String,String&gt; mm:regionJobsMap.entrySet()){
System.out.println(mm.getKey() +&quot;  &quot; + 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&lt;String,String&gt; hm= new HashMap&lt;String,String&gt;();
List&lt;WebElement&gt; locations = driver.findElements(By.xpath(&quot;//div[@data-metadata-id=&#39;locations&#39;]//div[@data-automation-id=&#39;facetValue&#39;]&quot;));  //get list of locations
for(int i=0; i&lt;locations.size();i++) //iterate over list
{
String cities = locations.get(i).findElement(By.cssSelector(&quot;label[id*=CheckBox-locations]&quot;)).getText(); //get location name
String jobNumber = locations.get(i).findElement(By.cssSelector(&quot;span[id*=CountLabel-locations]&quot;)).getText().replaceAll(&quot;[^0-9]&quot;, &quot;&quot;); // 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()+&quot;  =&gt;  &quot;+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.

huangapple
  • 本文由 发表于 2020年5月5日 06:16:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61602546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定