1

I am trying to get the value of SCN from my HTML page which is in this format-

<html>
    <body>
        <div class="hs-customerdata hs-customerdata-pvalues">
            <ul>
                <li class="hs-attribute">
                    <map-hs-label-value map-hs-lv-label="ACCOUNTINFO.SCN" map-hs-lv-value="89862530">
                    <span class="hs-attribute-label" hs-context-data="" translate="" hs-channel="abcd" hs-device="desktop">SCN:</span>
                    <span ng-bind-html="value | noValue | translate : params" class="hs-attribute-value" context-data="" map-v-key="89862530" map-v-params="" hs-channel="abcd" hs-device="desktop">
                    89862530</span>
                    </map-hs-label-value>
                </li>
            </ul>
        </div>
    </body>
</html>

As of now I tried different ways, but unable to reach the span and get the SCN value.

I tried -

scn = self.driver.find_elements_by_xpath(".//span[@class = 'hs-attribute-value']") 

which gives ElementNotFound error. The closest I have come to is -

div_element = self.driver.find_element_by_xpath('//div[@class="hs-customerdata hs-customerdata-personal"]/ul/li[@class="hs-attribute"]')

And then when I do -

print(div_element.get_attribute('innerHTML')) 

I get -

<map-hs-label-value map-hs-lv-label="ACCOUNTINFO.SCN" map-hs-lv-value="{{::customerData.details.scn}}"></map-hs-label-value>

But I am not able to go beyound this. I am new to using Webdriver and not able to figure out this. Any help would be much appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
MuCh
  • 103
  • 1
  • 9

2 Answers2

1

The value of SCN i.e. 89862530 is reflected in 3 different places and you can extract it from either of the places inducing WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • <map-hs-label-value> tag with map-hs-lv-value attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located()((By.XPATH, "//div[@class='hs-customerdata hs-customerdata-pvalues']/ul/li/map-hs-label-value"))).get_attribute("map-hs-lv-value"))
    
  • <span> tag with map-v-key attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located()((By.XPATH, "//div[@class='hs-customerdata hs-customerdata-pvalues']/ul/li/map-hs-label-value//span[@class='hs-attribute-value']"))).get_attribute("map-v-key"))
    
  • <span> tag with text as 89862530:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located()((By.XPATH, "//div[@class='hs-customerdata hs-customerdata-pvalues']/ul/li/map-hs-label-value//span[@class='hs-attribute-value']"))).get_attribute("innerHTML"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0
  1. You can locate the span element with text SCN: as //span[text()='SCN:']
  2. The element with the text of 89862530 would be the following-sibling of the element from point 1
  3. Putting everything together:

    driver.find_element_by_xpath("//span[text()='SCN:']/following-sibling::span").text
    

    Demo:

    enter image description here

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • I am still getting ElementNotFound with this. I think the WebDriverWait is required here. That solved the issue. Thanks for the documentation links though. – MuCh Jun 12 '19 at 23:11