1

I am trying to extract the string : "Plan B Warranty & Breakdown Recovery"

from the following block of code. I am having problems locating it via xpath and get text. I would be grateful for some pointers.

<!DOCTYPE html>
<html class="ng-scope" ng-app="dan">
<head>
<body class="u-scroll-y ng-scope" ng-controller="CoreController as cc">
<div class="c-animate c-animate--show u-pos-f-t ng-hide" ng-show="global.alerts.length">
<div class="o-grid-fluid u-h-100 u-pl-0">
<div class="o-grid__row u-ml-0 u-h-100">
<div class="c-loader ng-hide" ng-show="loadingHome" style="">
<nav class="o-grid__col-xl-2 o-grid__col-lg-3 o-grid__col-xs-4 u-p-0 c-card__block u-h-100 u-shadowed u-pos-f-t ng-scope" ng-if="global.loggedIn">
<div class="u-p-0 u-h-100 o-grid__col-xl-10 o-grid__col-lg-9 o-grid__col-xs-8 u-pull-right" ng-class="{ 'o-grid__col-xl-10 o-grid__col-lg-9 o-grid__col-xs-8 u-pull-right' : global.loggedIn }">
<header class="o-layout-table__row u-bg-primary u-shadowed u-clearfix u-px ng-scope" ng-if="global.loggedIn">
<main class="o-view-container">
<div class="o-grid-fluid u-py-md">
<div class="o-grid__row">
<div class="c-animate c-animate--view-slide-in-right c-animate--view-slide-out-right ng-scope" ng-view="" style="">
<div class="o-grid__col-md-10 o-grid__col-xl-8 o-grid__col-xl-offset-2 o-grid__col-md-offset-1 ng-scope">
<div class="u-mb-lg u-text-center">
<h1 class="u-text-bold">Recommendations</h1>
</div>
<form class="ng-pristine ng-valid ng-valid-min ng-valid-max ng-valid-required" name="recommend" ng-submit="recommend.$valid">
<div class="o-media c-card c-card__block u-shadowed u-mb-lg ng-scope" ng-if="rc.WarrantyEligible && !rc.prevWarranty()">
<label class="c-form-control-label u-px u-py-sm u-w-100">Warranty Options:</label>
<div class="c-form-group u-p-0 u-mb-sm u-clearfix">
<div class="o-grid__col-md-8">
<label class="c-form-control-label u-text-normal">Product Recommendations (Years):</label>
</div>
<div class="o-grid__col-md-4">
<input class="c-form-control ng-pristine ng-untouched ng-valid ng-valid-min ng-valid-max ng-valid-required" required="" placeholder="Years" ng-model="rc.recommend.year" min="1" max="3" type="number">
</div>
</div>
<div class="c-form-group ng-scope" ng-if="!data.answer_taxi">
<label class="c-option c-option--right u-px u-py-sm u-clearfix ng-scope" ng-if="!rc.planA && !rc.prestige" ng-click="cc.utils.audit('recommendation_warranty_plan_b')">
<input class="ng-pristine ng-untouched ng-valid ng-valid-required" required="" ng-model="rc.recommend.warrantyPlan" value="planB" name="warrantyPlan" type="radio">
<i class="c-option__radio"></i>
Plan B Warranty & Breakdown Recovery
</label>
</div>
</div>
<div class="o-media c-card c-card__block u-shadowed u-mb-lg u-text-body u-bg-success" ng-if="!rc.prevVap() && rc.VapEligible ">
<div class="c-form-group">
<label class="c-form-control-label u-px u-py-sm u-w-100">Vehicle Asset Protection Options:</label>
<label class="c-option c-option--right u-px u-py-sm u-clearfix" ng-click="cc.utils.audit('recommend_vap')">
<input class="ng-pristine ng-untouched ng-valid ng-valid-required" required="" ng-model="rc.recommend.vapPlan.plan" value="standard" name="vapPlan" type="radio">
<i class="c-option__radio"></i>
Vehicle Asset Protection - Standard Cover
</label>
<label class="c-option c-option--right u-px u-py-sm u-clearfix" ng-click="cc.utils.audit('vap_key_facts_checked')">
<input class="ng-pristine ng-untouched ng-valid ng-valid-required" required="" ng-model="rc.recommend.vapCheck" name="vapCheck" type="checkbox">
<i class="c-option__checkbox"></i>
You confirm that you have received the VAP key facts leaflet?
</label>
</div>
</div>
</form>
<div class="c-form-group">
<input class="c-btn c-btn--primary c-btn--block u-pull-right" value="Complete and Preview" ng-click="rc.complete(recommend.$valid); cc.utils.audit('recommend_complete')" ng-disabled="recommend.$invalid" type="submit">
</div>
</div>
</div>
</div>
</div>
</main>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js">
<script src="./build/app.js?v=2.13" defer="">
<script src="/build/standalone/jspdf.js" defer="">
<script src="/build/standalone/sigWebTablet.js" defer="">
</body>
</html>

ignore this ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3 Answers 3

1

To extract the string "Plan B Warranty & Breakdown Recovery" you can use the following code block :

System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[@class='c-option c-option--right u-px u-py-sm u-clearfix ng-scope' and not(@class='ng-pristine ng-untouched ng-valid ng-valid-required') and not (@class='c-option__radio')]"))).getAttribute("innerHTML"));

As an alternative, you can try this code block as well :

WebDriverWait wait20 = new WebDriverWait(driver, 20);
WebElement ele = wait20.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[@class='c-option c-option--right u-px u-py-sm u-clearfix ng-scope' and not(@class='ng-pristine ng-untouched ng-valid ng-valid-required') and not (@class='c-option__radio')]")));
System.out.println(ele.getAttribute("innerHTML"));
Sign up to request clarification or add additional context in comments.

2 Comments

Corrected the xpath error, please let me know the status
Thank you both. Using the "Innerhtml" solution i can now extract the string from the result.
1

First get the element by classname. Then get the text from it.

EDIT:

WebElement element = driver.findElement(By.xpath("//label[@class='c-option c-option--right u-px u-py-sm u-clearfix ng-scope' and not(@class='ng-pristine ng-untouched ng-valid ng-valid-required') and not (@class='c-option__radio')]"));  
String desiredText = element.getAttribute("innerHTML"); 
System.out.println(desiredText);

10 Comments

I think the text is attached to label tag. i tag is closed and does not contain text
Bu using the provided html i got Success to get the desired String using the code above.
Hi Mark Let me know your feedback.
hi, response 1 failed : SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//label[@class='c-option c-option--right u-px u-py-sm u-clearfix ng-scope' and not(@class='ng-pristine ng-untouched ng-valid ng-valid-required') and not (@class='c-option__radio')' is not a valid XPath expression. response 2 failed : no such element: Unable to locate element: {"method":"class name","selector":"c-option__radio"}
@MarkH is there any other radio option in your page.
|
0

The following xpath worked for me:

//input[@ng-model='rc.recommend.warrantyPlan']/following-sibling::i[1]

It's finding the associated input element, then grabbing the first i element after it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.