只从URL获取id

时间:2018-01-09 07:37:28

标签: java selenium-webdriver

已搜索,无法找到以下问题的确切解决方案。也就是说,我有很多看起来像这样的网址:

https://{domain name}/{type of data}/4583236-{name-of-perpetrators}

我的任务是从上面的网址中提取ID。试了这个没有成功:

dataPosted.get(l).getAttribute("data-user-id");

以上get(l)来自计数器,下面是该特定URL的HTML:

<div class="list-box-user-action">
 <span><a href="https://{domain name}/{type of data}/4583236-{name-of-perpetrators}" class="">Title</a></span>

此处还有来自页面来源的屏幕截图

enter image description here

提前谢谢

2 个答案:

答案 0 :(得分:1)

首先,您需要从StringURL标记的href属性中提取<a>,如下所示:

  • String中提取https://{domain name}/{type of data}/4583236-{name-of-perpetrators} URL

    String my_href = driver.getCurrentUrl();
    
  • String标记的https://{domain name}/{type of data}/4583236-{name-of-perpetrators}属性中提取href <a>

    String my_href = driver.findElement(By.xpath("//div[@class='list-box-user-action']/span/a[contains(.,'Title')]"));
    
  • 现在您可以拆分String https://{domain name}/{type of data}/4583236-{name-of-perpetrators} 以提取 4583236

    String[] urlParts = my_href.split(Pattern.quote("{type of data}/"));
    String mySuburl = urlParts[1];
    String[] suburl = mySuburl.split(Pattern.quote("-{name-of-perpetrators}"));
    String mytext = suburl[0];
    System.out.println(mytext);
    

答案 1 :(得分:0)

修正了以下RegEx:

Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(link);
String g = m.group();