gpt4 book ai didi

java - xpath可以用来搜索网站的链接吗?

转载 作者:行者123 更新时间:2023-12-01 13:36:10 25 4
gpt4 key购买 nike

我正在寻找诸如查找网页中是否存在链接之类的功能。如果链接存在,则打印一些链接“找到链接!”。

示例:(我想找到 XPATH“123”)

driver.findElement(By.xpath ("123"));
if (the xpath found the "123"){
System.out.printIn ("link found")
} else {
System.out.printIn ("missing link..")
}

最佳答案

假设您正在使用Selenium Webdriver

来自API :

WebElement findElement(By by)

Find the first WebElement using the given method. This method is affected by the 'implicit wait' times in force at the time of execution. The findElement(..) invocation will return a matching row, or try again repeatedly until the configured timeout is reached. findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.

所以你的代码可能看起来像这样:

List<WebElement> allElements = driver.findElements(By.xpath("123"));
if (allElements == null || allElements.size() == 0) {
System.out.printIn ("missing link..")
} else {
System.out.printIn ("link found")
}

关于java - xpath可以用来搜索网站的链接吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21253778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com