Can anyone help me to derive the xpath (from second div the span element label which is GP)
<div class="ohg-patient-banner-suppl-info-section ohg-patient-banner-suppl-info-custom pure-u-1-5"> <div class="ohg-patient-banner-suppl-info-component-container ohg-patient-banner-suppl-info-component-left-bordered ohg-patient-banner-suppl-info-custom" aria-label="Visit Info" role="group"><div class="ohg-patient-banner-suppl-info-section-summary" aria-hidden="false"> </div> <div class="ohg-patient-banner-suppl-info-section-detail" aria-label="" aria-hidden="true"> <div class="ohg-patient-banner-suppl-info-custom-field"> <span class=" " aria-hidden="true"></span> <span class=" ohp-metadata-label">Location</span> <span class="ohg-patient-banner-suppl-info-custom-row-value-icon " aria-hidden="true"></span> <span class=" ohg-patient-banner-suppl-info-value">Tauranga Hospital - Assmt Plan Unit TAU - </span> </div> </div> </div> </div> <div class="ohg-patient-banner-suppl-info-section ohg-patient-banner-suppl-info-custom pure-u-1-5"> <div class="ohg-patient-banner-suppl-info-component-container ohg-patient-banner-suppl-info-component-left-bordered ohg-patient-banner-suppl-info-custom" aria-label="GP Info" role="group"><div class="ohg-patient-banner-suppl-info-section-summary" aria-hidden="false"> </div> <div class="ohg-patient-banner-suppl-info-section-detail" aria-label="" aria-hidden="true"> <div class="ohg-patient-banner-suppl-info-custom-field"> <span class=" " aria-hidden="true"></span> <span class=" ohp-metadata-label">GP</span> <span class="ohg-patient-banner-suppl-info-custom-row-value-icon " aria-hidden="true"></span> <span class=" ohg-patient-banner-suppl-info-value">-</span> </div> </div> </div> </div>
My XPath which i wrote it work for the first div and it return the value Location :
.//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class=' ohp-metadata-label']
Xpath:
//*[@class='ohg-patient-banner-suppl-info-custom-field']//span[2]
then you can use gettext() to get GP as a text
Try this XPath-1.0 expression:
//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field' and span[@class=' ohg-patient-banner-suppl-info-value']='-']/span[@class=' ohp-metadata-label']
Its result is:
GP
your xpath actually does return both elements:
in your selenium lib you receive most likely an array and can select the second element of it
if its always the second element adding a [2]
to the xpath helps eg "(.//*[@class='ohg-patient-banner-suppl-info-section-detail'])[2]/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class=' ohp-metadata-label']"
If you have some text in the page that is fixed, eg Location
you can use that as reference and then using ancestor
and sibling
axes
".//span[.='Location']//ancestor::div[@class='ohg-patient-banner-suppl-info-section-detail']//following-sibling::div[@class='ohg-patient-banner-suppl-info-section-detail']//span[@class=' ohp-metadata-label']"
Since you don't have other unique identifiers, and the class name is used by 2 spans, this is how you can identify that span based on its index, and is the shortest way:
xpath: (//span[@class=' ohp-metadata-label'])[2]
Now you can scrape the text by using selenium getText() method. Note that we used index 2 to identify your locator, but if html code will change, and new similar spans will be added before this one, you will need to change the index.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.