我想在这个html代码中获取所有数据源网址。我不知道如何才能使用此代码..任何人都可以帮助我
<ul class="resp-tabs-list">
<li class="resp-tab-item resp-tab-active" aria-controls="tab_item-0" role="tab">
<a class="js-tab-list js-tab-1" data-source="https://www.adsdssas.com/property/individual/BC-2030877?isl=1" data-no="1" data-ga="0" target="iframe_1">
<img src="/themes/centralization/images/partners-tab-logo/rentalhomes-tab-logo.png" alt="RentalHomes.com" height="20" width="auto">
</a>
</li>
<li class="resp-tab-item" aria-controls="tab_item-1" role="tab">
<a class="js-tab-list js-tab-1" data-source="https://www.jdoasdadadqocy.com/click-8111003-11553823?url=https%3A%2F%2Fwww.vrasbo.com%2Fvacation-rentals%3Fq%3DDauphin%2BIsland%252C%2BAL%252C%2BUSA&utm_campaign=RENTALHOMES&sid=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&label=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&ft=1" data-no="1" data-ga="0" target="iframe_1">
<img src="/themes/centralization/images/partners-tab-logo/feed_logo_vrbo.png" alt="VRBO.Com" height="20" width="auto">
</a>
</li>
<li class="resp-tab-item" aria-controls="tab_item-2" role="tab">
<a class="js-tab-list js-tab-1" data-source="https://www.jdadsdoqocy.com/click-8111003-10859031?url=https%3A%2F%2Fwww.homeaway.com%2Fresults%2Fkeywords%3ADauphin%2BIsland%252C%2BAL%252C%2BUSA&utm_campaign=RENTALHOMES&sid=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&label=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&ft=1" data-no="1" data-ga="0" target="iframe_1">
<img src="/themes/centralization/images/partners-tab-logo/feed_logo_12.png" alt="HomeAway.Com" height="20" width="auto">
</a>
</li>
<li class="resp-tab-item" aria-controls="tab_item-3" role="tab">
<a class="js-tab-list js-tab-1" data-source="https://www.dddoaoa.com/click-8111003-11327743?url=https://www.asdasd.com/search.do?q-destination=Dauphin+Island%2C+AL%2C+USA&utm_campaign=RENTALHOMES&sid=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&label=MTUwMTE0MTI4NDIwMi41LjUwLjQzLTM&ft=1" data-no="1" data-ga="0" target="iframe_1">
<img src="/themes/centralization/images/partners-tab-logo/feed_logo_14.png" alt="Hotel.Com" height="20" width="auto">
</a>
</li>
尝试使用此代码,现在一切看起来都很好
for (String windows : wd.getWindowHandles()) {
wd.switchTo().window(windows);
if (wd.getCurrentUrl().startsWith("http://something.com")) {
List<WebElement> url = wd.findElements(By.xpath("//li/a"));
for(WebElement now : url)
{
System.out.println(now.getAttribute("data-source"));
}
}
}
答案 0 :(得分:1)
这个想法是收集所有的&#34; li&#34; webelements。然后循环遍历每个并获取其data-source属性的值。
List<WebElement> url = driver.findElements(by.xpath("//li/a"));
for(WebElement now : url)
{
System.out.println(now.getArrtibute("data-source"));
}
希望这会有所帮助。感谢。