我正在尝试从以下html中提取href值<a ng-href=
。
价值&#34; BJ11TFZ&#34;是动态的。
这是一个有很多LI的UL,它将是列表中的第一项。 我希望能够捕获它,以便我可以构建一个URL来点击
<!-- Begin Results List -->
<ul id="resultsLists" class="o-media-list c-animate c-animate--show" ng-show="rc.results.vehicles.length" style="">
<li id="BJ11TFZ" class="c-animate c-animate--repeat u-pb ng-scope" ng-repeat="car in rc.results.vehicles track by car.registration" style="">
<div class="o-media c-card c-card--hover c-card__block u-p-0 u-shadowed u-shadowed--hover">
<div class="o-media__left o-grid__col-sm-5 o-grid__col-xs-12 u-p-0 u-no-float--sm">
<a ng-href="/used-car/FORD/FIESTA/BJ11TFZ" class="o-embed-responsive o-embed-responsive--16by9 o-media__object c-rollover" ng-click="rc.viewCar(car, $index)" href="/used-car/FORD/FIESTA/BJ11TFZ">
<img ng-src="//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-S-01.jpg" alt="FORD FIESTA" class="o-embed-responsive__item c-rollover__image" cs-src-responsive="[ [ 'small', '//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-M-01.jpg' ], [ 'retina', '//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-M-01.jpg' ] ]" src="//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-M-01.jpg">
</a>
</div>
<div class="o-media__body o-media--middle c-card">
<label class="c-option c-option--stacked u-pos-t-r u-text-primary u-m-sm u-hidden-print">
<input checklist-value="car.registration" ng-model="checked" class="ng-scope ng-pristine ng-untouched ng-valid" checklist-model="search.savedVehicles" type="checkbox">
<i class="c-option__heart"></i>
<span class="u-text-70">Save</span>
</label>
<div class="c-card__block" ng-click="rc.viewCar(car, $index)">
<strong ng-if="car.vehicleCapDetails.capMakeName != 'MERCEDES-BENZ'" class="ng-binding ng-scope">
FORD FIESTA
</strong>
<div class="u-text-strong u-text-small ng-binding">1.6 TDCi [95] Titanium 3dr, 2011</div>
<div class="u-text-bold u-lh-1 u-pt-sm ng-scope" ng-if="!search.criteria.fromPrice && !search.criteria.toPrice">
<small>From</small> <span class="u-text-125 ng-binding">£114</span> <small>monthly or</small>
<span ng-if="!car.vehiclePrice.exVatPrice" class="ng-binding ng-scope">£5,071</span>
</div>
<div class="u-text-70 u-text-muted u-text-nowrap u-py-sm"> + £149 Admin Fee</div>
<ul class="o-list-inline u-text-70 u-text-muted">
<li class="o-list-inline__item u-pl-0 u-pr-sm">
<span ng-if="car.mileage > 1000" class="ng-binding ng-scope">
67k miles
</span>
</li>
<li class="o-list-inline__item u-pl-0 u-pr-sm ng-binding">
Diesel
</li>
<li class="o-list-inline__item u-pl-0 u-pr-sm ng-binding">
Manual
</li>
<li class="o-list-inline__item u-pl-0 u-pr-sm ng-binding">
Red
</li>
<li class="o-list-inline__item u-pl-0 ng-binding">
<i class="i-icon i-icon--location"></i> Norton<span class="u-text-strong ng-binding ng-scope" ng-if="car.distance">, 2mi</span>
</li>
</ul>
</div>
</div>
</div>
答案 0 :(得分:1)
如果li元素的类名是唯一的,您可以使用:
WebElement resultsLists= driver.findElement(By.id("resultsLists"));
WebElement el=resultsLists.findElement(By.xpath(".//li[@class='c-animate c-animate--repeat u-pb ng-scope']/div/div/a"));
System.out.println(el.getAttribute("href"));
否则,您可以使用索引来访问该元素。如果是第一个:
WebElement resultsLists= driver.findElement(By.id("resultsLists"));
WebElement el=resultsLists.findElement(By.xpath(".//li[1]/div/div/a"));
System.out.println(el.getAttribute("href"));
答案 1 :(得分:0)
您可以在复杂的xpath上尝试这些简化的CSS
。由于缺乏唯一标识符,它假设某种结构,这是不幸的。您可以向开发人员(或您自己,如果您是开发人员)询问独特的类/ ID,以获得更好的可测试性和稳定的自动化套件。
说明:使用ul
id
转到resultsLists
,在其下方找到li
类c-animate
。然后在其下方的“任何地方”找到a
(超链接)。
WebElement link= driver.findElement(By.css("ul#resultsLists>li.c-animate a"));
System.out.println(link.getAttribute("href"));
说明:使用ul
id
转到resultsLists
,找到div
,其中o-media__left
类“位于”下方。然后在其下方“立即”找到a
(超链接)。
WebElement link= driver.findElement(By.css("ul#resultsLists div.o-media__left>a"));
System.out.println(link.getAttribute("href"));
答案 2 :(得分:0)
要从ng-href
标记中提取动态的href
和<a ng-href="/used-car/FORD/FIESTA/BJ11TFZ" ... >
值“BJ11TFZ”,您可以考虑以下代码:
WebElement my_model = driver.findElement(By.xpath("//ul[@id='resultsLists' and @class='o-media-list c-animate c-animate--show']//following::a[1]"));
//Extracting ng-href value
System.out.println(my_model.getAttribute("ng-href"));
//Extracting href value
System.out.println(my_model.getAttribute("href"));