从java获取链接文本

从java获取链接文本,java,html,selenium,Java,Html,Selenium,我在获取链接文本时遇到问题 在网站上我有文本链接 ,带有ZZ-的部分是静态的,数字递增,我之前不知道。我需要这个号码 我以前看的是:但是我有所有的链接,URL(不是链接的文本) 我也尝试过:但每次我改变并寻找解决方案时,我都会输出打印null 解决方案:也不好,因为它不能识别“a[href*='ZZ-'” 因此,最接近的是: List<WebElement> elements = driver.findElements(By.tagName("a")); for (int i =

我在获取链接文本时遇到问题

在网站上我有文本链接
,带有
ZZ-
的部分是静态的,数字递增,我之前不知道。我需要这个号码

我以前看的是:但是我有所有的链接,URL(不是链接的文本)

我也尝试过:但每次我改变并寻找解决方案时,我都会输出
打印null

解决方案:也不好,因为它不能识别
“a[href*='ZZ-'”

因此,最接近的是:

List<WebElement> elements = driver.findElements(By.tagName("a")); 
for (int i = 0; i < elements.size(); i++) {
   System.out.println(elements.get(i).getAttribute("href"));
}
List elements=driver.findElements(按.tagName(“a”));
对于(int i=0;i

但我怎样才能改变,不仅查看URL,而且查看链接的名称呢?(特别是从
ZZ-
开始的元素)

来定位您可以使用的元素

List<WebElement> elements = driver.findElements(By.partialLinkText("ZZ"));
// or
List<WebElement> elements = driver.findElements(By.cssSelector("[href*='ZZ']"));

您好,您可以使用以下代码提取号码:

public String splitfunc(String str)
{
    str = str.replace(".html", "");
    String[] array = str.split("-"); 
    return array[1];
}


  List<WebElement> elements = driver.findElements(By.tagName("a")); 
  for (int i = 0; i < elements.size(); i++) {
   System.out.println(splitfunc(elements.get(i).getAttribute("href")));
}
publicstringsplitfunc(stringstr)
{
str=str.replace(“.html”,”);
字符串[]数组=str.split(“”);
返回数组[1];
}
列表元素=driver.findElements(按.tagName(“a”));
对于(int i=0;i
由于您要查找的是链接的文本,而不是实际的href URL本身,因此我认为抓取元素文本并将其用于解析而不是拉出href属性更为简洁,更不容易出错。然后,如果文本总是以
zzsomenumber
的形式出现,那么您可以使解析相当简单

使用Java 8的示例(假设已经创建了驱动程序并加载了正确的页面):

String leadingStr=“ZZ-”;
Listnumbers=driver.findElements(按.tagName(“a”))
.stream()
.map(WebElement::getText)
.filter(str->null!=str&&str.startsWith(leadingStr))
.map(str->str.replace(leadingStr,“”.trim())
.filter(str->!str.isEmpty())
.map(整数::valueOf)
.collect(Collectors.toList());
没有流的示例:

String leadingStr = "ZZ-";
List< Integer > numbers = new ArrayList<>();
for (WebElement elem : driver.findElements(By.tagName("a"))) {
    String text = elem.getText();
    if (text.startsWith(leadingStr)) {
        numbers.add(Integer.valueOf(text.replace(leadingStr,"").trim()));
    }
}
String leadingStr=“ZZ-”;
Listnumbers=newarraylist();
对于(WebElement元素:driver.findElements(按.tagName(“a”))){
String text=elem.getText();
if(文本起始带(引线TR)){
number.add(Integer.valueOf(text.replace(leadingStr,”).trim());
}
}

当然,如果假设它们总是以
ZZ someNumber
的形式出现,则上述两种情况都需要更多的错误处理,但这只是围绕整数转换添加一些try-catch块,等等。

如果您的语言水平允许,一种更优雅的方法是使用foreach,以避免索引越界异常的风险

这将允许您根据请求获取链接的文本,而不是href并进行大量解析

修剪只是额外的防御性编码

List<WebElement> links = driver.findElements(By.tagName("a")); 
for (WebElement link : links ) {
   System.out.println(link.getText().replace("ZZ-","").trim());
}
List links=driver.findElements(按.tagName(“a”));
用于(WebElement链接:链接){
System.out.println(link.getText().replace(“ZZ-”,“).trim());
}

我认为这是从锚点获取文本的最简单方法之一

WebElement link  = driver.findElement(By.partialLinkText("ZZ"));
System.out.println(link.getText());

这个问题的确切解决方案如下:

正如您所提到的,您希望在-

为此,可以使用xpath中提供的start来匹配开始文本

 List<WebElement> elements = driver.findElements(By.xpath("//a[starts-with(text(),'ZZ-')]")); 
  for (int i = 0; i < elements.size(); i++) {
   System.out.println(elements.get(i).getAttribute("href")));  
List elements=driver.findElements(By.xpath(“//a[以(text(),'ZZ-')]开头]);
对于(int i=0;i

@Gupta的答案是一个很好的破解方法。在我看来,这不是关于selenium的正确解决方案。

您可以尝试通过.xpath查找链接的文本:
//a[contains(href,“DetailsZZ-”)
它工作得非常好,我只是在
字符串[]data=text.split(“-”)上得到了NullPointerException;
行,所以我要找出发生了什么:)@Michal使用两个版本的文本提取?效果几乎很好,我想我知道你想展示什么:)我刚刚得到了
“java.lang.ArrayIndexOutOfBoundsException:1”
返回错误,所以我只需要找出发生了什么(但谢谢你的提示:))@Michal:我猜有一些
a
元素的
href
属性中没有数值;类似于:“DetailsZZ”或“DetailsZZ-”或“”,这就是它抛出ArrayOutOfBoundsException的原因,因为数组中没有第二个值可显示。请按照@Andersson的评论,在上面的代码中尝试,替换为以下任一
List elements=driver.findElements(by.xpath(//a[contains(@href,“DetailsZZ-”);
List elements=driver.findElements(by.xpath(//a[以(href,“DetailsZZ-”)开头];
你们很棒,这就是它应该做的!谢谢!但对于未来,如果有人来看这里,它应该是
'DetailsZZ-'
而不是
“DetailsZZ-”
:)@Michael…这对我来说听起来是一个数组问题,即数组[1]…当数组的值不存在并且我们试图获取它时,可能会发生这种情况..我不确定,但是element.element.getText()不应编译,因为element不是WebElement中的公共字段。
WebElement element = driver.findElement(By.partialLinkText("ZZ-10048"));
String txt = element.getText();
String[] words = txt.split("-");
System.out.println(words[1]);
WebElement link  = driver.findElement(By.partialLinkText("ZZ"));
System.out.println(link.getText());
 List<WebElement> elements = driver.findElements(By.xpath("//a[starts-with(text(),'ZZ-')]")); 
  for (int i = 0; i < elements.size(); i++) {
   System.out.println(elements.get(i).getAttribute("href")));