John S
John S

Reputation: 85

Get text from div using Selenium in a dynamically named class

<div class="jss14 jss41">
 <div class="rn-obd0qt rn-1efd50x rn-14skgim rn-rull8r rn-mm0ijv rn-13yce4e rn-fnigne rn-ndvcnb rn-gxnn5r rn-deolkf rn-6koalj rn-1qe8dj5 rn-1mlwlqe rn-eqz5dr rn-1h0z5md rn-1mnahxq rn-61z16t rn-p1pxzi rn-11wrixw rn-ifefl9 rn-bcqeeo rn-wk8lta rn-9aemit rn-1mdbw0j rn-gy4na3 rn-bnwqim rn-1lgpqti">
   <div color="#777" dir="auto" class="rn-13yce4e rn-fnigne rn-ndvcnb rn-gxnn5r rn-deolkf rn-1471scf rn-1b43r93 rn-o11vmf rn-ebii48 rn-t9a87b rn-1mnahxq rn-61z16t rn-p1pxzi rn-11wrixw rn-wk8lta rn-9aemit rn-1mdbw0j rn-gy4na3 rn-bauka4 rn-q42fyq rn-qvutc0" style="color: rgb(119, 119, 119); font-family: Roboto, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-weight: 400; text-align: justify;">$6.49</div>
 </div>
</div>

I am looking to extract the $6.49 value.

I have only found posts that have shown how to do this when the class name is static, such as:

elements = browser.find_elements_by_class_name('_2v66')

for e in elements:
    print(e.text)

How to approach this when the class name is dynamically created on click?

Edit:

More outerHTML as requested: enter image description heres://i.sstatic.net/m65D8.png

Upvotes: 0

Views: 807

Answers (1)

Navarasu
Navarasu

Reputation: 8489

When we don't find any unique attribute matching, then we can go with dom parent/sibiling structure or displayed text value or combining to get the unique non changing locator.

For example,

  • Here div has two immediate parent. we can take this a criteria. so div>div>div css locator will reduced result.

  • Then I guess dir="auto" is not changing, we can include that as well. So the css will be div>div>div[dir='auto']

  • If value is displayed, definitely it may have label preceding it.

Amount : $6.49

For e.g. If you have label before the div.

    <label> Amount</label>
    <div class="jss14 jss41">
      <div class="rn-obd0qt rn-1efd50x rn-14skgim rn-rull8r rn-mm0ijv rn-13yce4e rn-fnigne rn-ndvcnb rn-gxnn5r rn-deolkf rn-6koalj rn-1qe8dj5 rn-1mlwlqe rn-eqz5dr rn-1h0z5md rn-1mnahxq rn-61z16t rn-p1pxzi rn-11wrixw rn-ifefl9 rn-bcqeeo rn-wk8lta rn-9aemit rn-1mdbw0j rn-gy4na3 rn-bnwqim rn-1lgpqti">
       <div color="#777" dir="auto" class="rn-13yce4e rn-fnigne rn-ndvcnb rn-gxnn5r rn-deolkf rn-1471scf rn-1b43r93 rn-o11vmf rn-ebii48 rn-t9a87b rn-1mnahxq rn-61z16t rn-p1pxzi rn-11wrixw rn-wk8lta rn-9aemit rn-1mdbw0j rn-gy4na3 rn-bauka4 rn-q42fyq rn-qvutc0" style="color: rgb(119, 119, 119); font-family: Roboto, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-weight: 400; text-align: justify;">$6.49</div>
     </div>
    </div>

Then you can use that to find this element like,

//label[text() == 'Amount']/following-sibling::div/div/div

Please find the nearest or parent non changing value and traverse to the required element.

Upvotes: 1

Related Questions