0

I'm trying to get text present in Dom structure but I could't get it, I have used getText(); and getAttribute(); but not getting I have used value,inner html,outer html,text and so on which has to be mention in getAttribute();but unable to get that text could any one help me to get from it.

<label class="">
    <input type="checkbox" value="ml+4gvbuEG8Y3bBbAEbQvA==" name="selectedemployees" class="checkall">Alberta              Weekly1             
</label>

I want to get "Alberta Weekly1" text in my console

enter image description here

Console output

3 Answers3

0

To extract the text Alberta Weekly1 you need to induce WebDriverWait for the visibilityOfElementLocated() and as the desired node is a text node you can use executeScript() method along with either of the following Locator Strategies:

  • xpath 1:

    System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[./input[@class='checkall' and @name='selectedemployees']]")))).toString());
    
  • xpath 2:

    System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label/input[@class='checkall' and @name='selectedemployees']/..")))).toString());
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

To get the Text Alberta Weekly1 You need to induce WebDriverWait And visibilityOfElementLocated() And use the below XPATH Option.

WebDriverWait wait = new WebDriverWait(driver, 30);
String textval=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[./input[@name='selectedemployees']]"))).getText(); 
System.out.println(textval);
KunduK
  • 26,790
  • 2
  • 10
  • 32
  • i'am getting only Alberta Weekly1 text, i have to get all text mentioned in unique way – phani kumar Sep 25 '19 at 14:01
  • @phanikumar : Your requirement was to get the text `Alberta Weekly1` .If you want to fetch all the text you need to post DOM element for all the text you are after? – KunduK Sep 25 '19 at 14:04
  • you are right but the xpath is written for all elements //label[./input[@name='selectedemployees']] .not for single element – phani kumar Sep 25 '19 at 14:11
  • So when you run that code what text you are getting? – KunduK Sep 25 '19 at 14:12
  • Check the following xpath : `//label[./input[@name='selectedemployees' and @class='checkall']]` – KunduK Sep 25 '19 at 14:28
  • plz check 'Console output' image on my query i have posted, i just want to get unique mentioned in application image, i have to get all check boxes names. – phani kumar Sep 26 '19 at 04:33
0

You can try below given xpath as well.

//label//child::input[@class='checkall' and @name='selectedemployees']
Zohair
  • 268
  • 2
  • 7