我有一個<ul>包含xpath:position = //ul[5]一些<a>.
第一個<a>有xpath:position = //ul[5]/li/div/div/a,下一個<a>有xpath:position = //ul[5]/li[2]/div/div/a,下一個有xpath:position = //ul[5]/li[3]/div/div/a,繼續……
所以,對于每一個新<a>進入這個<ul>的xpath:position獲得<a>一個[#]后<li>。

我需要的是一個例子,說明我將如何計算有多少<a>存在于這個特定的東西中<ul>,然后將href每個屬性的屬性<a>放入一個串列中。
我試過這個:
WebDriver driver = DriverFactory.getWebDriver()
def aCount = driver.findElements(By.xpath("//ul[5]/li/div/div/a")).size()
println aCount
但它計算了所有<a>頁面,而不僅僅是<ul>帶有 with的頁面xpath:position = //ul[5]!
uj5u.com熱心網友回復:
使用絕對 xpath 使測驗更少htmlchangeproof,更好地避免這些。
您只需要以下組合:
- 使用父/子元素使用
element.findElements(By.by) - 查找子元素
By.tagName(String tagName)
代碼示例:
package tests;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import selenium.ChromeDriverSetup;
public class CollectHrefsTest extends ChromeDriverSetup {
public static void main(String[] args) {
List<String> hrefs = new ArrayList<String>();
WebDriver driver = startChromeDriver(); // wrapped driver init
driver.get("https://www.stackoverflow.com");
List<WebElement> ulTags = driver.findElements(By.tagName("ul"));
for (WebElement ulTag: ulTags) {
List<WebElement> liTags = ulTag.findElements(By.tagName("li"));
for (WebElement liTag: liTags) {
List<WebElement> aTags = liTag.findElements(By.tagName("a"));
for (WebElement aTag: aTags) {
String href = aTag.getAttribute("href");
if (href != null) {
hrefs.add(href);
System.out.println(href);
}
else {
System.out.println("href is null");
}
}
}
}
System.out.println("hrefs collected: " hrefs.size());
driver.quit();
}
}
輸出:
Starting ChromeDriver 97.0.4692.71 (adefa7837d02a07a604c1e6eff0b3a09422ab88d-refs/branch-heads/4692@{#1247}) on port 13301
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1644849838.445][WARNING]: This version of ChromeDriver has not been tested with Chrome version 98.
úno 14, 2022 3:43:58 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
https://stackoverflow.com/
https://stackoverflow.com/help
https://chat.stackoverflow.com/?tab=site&host=stackoverflow.com
https://meta.stackoverflow.com/
https://stackoverflow.com/questions
https://stackoverflow.com/jobs
https://stackoverflow.com/jobs/directory/developer-jobs
https://stackoverflow.com/jobs/salary
https://stackoverflow.com/help
href is null
href is null
https://stackoverflow.com/teams
https://stackoverflow.com/talent
https://stackoverflow.com/advertising
https://stackoverflowsolutions.com/explore-teams
https://stackoverflow.co/
https://stackoverflow.co/company/press
https://stackoverflow.co/company/work-here
https://stackoverflow.com/legal
https://stackoverflow.com/legal/privacy-policy
https://stackoverflow.com/legal/terms-of-service
https://stackoverflow.co/company/contact
https://stackoverflow.com/#
https://stackoverflow.com/legal/cookie-policy
https://stackexchange.com/sites#technology
https://stackexchange.com/sites#culturerecreation
https://stackexchange.com/sites#lifearts
https://stackexchange.com/sites#science
https://stackexchange.com/sites#professional
https://stackexchange.com/sites#business
https://api.stackexchange.com/
https://data.stackexchange.com/
https://stackoverflow.blog/?blb=1
https://www.facebook.com/officialstackoverflow/
https://twitter.com/stackoverflow
https://linkedin.com/company/stack-overflow
https://www.instagram.com/thestackoverflow
hrefs collected: 35
uj5u.com熱心網友回復:
所有的<a>都在他們的祖先之內<li>,所有的<li>s都在里面//ul[5]。因此解決方案將是遍歷所有<li>s 并且您可以使用以下定位器策略:
WebDriver driver = DriverFactory.getWebDriver()
def aCount = driver.findElements(By.xpath("//ul[5]//li/div/div/a")).size()
//note the double slash here ^
println aCount
uj5u.com熱心網友回復:
問題是進入//ul[5]有兩種<a>s。//ul[5]/li/div/div/a和//ul[5]/li/div/div[2]/a。_
在第一種情況下<div>,包裝 的<a>具有類名 ( div[@]/a[1])。在第二種情況下<div>,包裝 的<a>具有類名 ( div[@]/a[1])。
當我計算<a>s 時,我同時計算了兩種<a>s。
所以我不得不做這樣的事情:
WebDriver driver = DriverFactory.getWebDriver()
List<String> hrefs = []
List<WebElement> aTags = driver.findElements(By.xpath('//ul[5]/li/div/div[@]/a'))
for (WebElement aTag in aTags) {
String href = aTag.getAttribute("href")
if (href != null) {
hrefs.add(href);
} else {
hrefs.add('Empty Link');
}
}
System.out.println(hrefs "\n\nURLs Found: " hrefs.size())
我使用的是:
findElements(By.xpath("//ul[5]/li/div/div/a"))
而不是:
findElements(By.xpath('//ul[5]/li/div/div[@]/a'))它只獲取由帶有“heading-4”<a>的 a 包裹的 s 。<div>class name
https://docs.katalon.com/katalon-studio/docs/detect_elements_xpath.html#what-is-xpath
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429876.html
標籤:爪哇 硒 摇摆 katalon工作室 katalon录音机
下一篇:單擊時按鈕的圖示不更新
