Reputation: 35
I would like to get the text(Which is null right now but get some text in future, so printing null should be fine for now) from second "109-top-dark-grey-block ng-binding" class . Tried tabIndex and nth-child both are not working. "
<div class="122-top-section-btm-half">
<div class="108-top-grey-m12x3"></div>
<div class="109-top-dark-grey-block ng-binding">ab ab xyz</div>
</div>
" "
<div class="d122-top-section-btm-half">
<div class="108-top-grey-m12x4"></div>
<div class="109-top-dark-grey-block ng-binding"></div>
"
Upvotes: 1
Views: 1091
Reputation: 480
To get the text of the second div block nth-child should work. I tested the selector locally in chrome tools:
So in your Java:
String elementText = driver.findElement(By.cssSelector(".d122-top-section-btm-half:nth-child(2) .ng-binding")).getText();
Should do the trick - as the CSS spec says nth-child is 1 indexed - not 0 - so its the 2nd child.
Based on the HTML snippet you provided you could use a CSS selector. So you could do:
String elementText = driver.findElement(By.cssSelector(".d122-top-section-btm-half .109-top-dark-grey-block")).getText();
Or if you are just after the element with the ng-binding within your first div then it would be cleaner:
String elementText = driver.findElement(By.cssSelector(".d122-top-section-btm-half .ng-binding")).getText();
Both would return the element text - maybe take a look at CSS Selectors Guide to learn more.
Upvotes: 2