Reputation: 159
Sometimes I failed to get the inner text of a web-element; recently working on thePersonal insurance, and failed to get inner text of label (web element). Here are the script and screenshot of webpage inspected:
WebElement detailPostCode =driver.findElement(By.xpath("//label[@for='q_codePostalDetailAU']"));
System.out.println("postcode label text "+detailPostCode.getText());
Could any please help me understand the problem. Thank you for your kind concern.
Upvotes: 0
Views: 2112
Reputation: 193188
As per the HTML you have shared the <label>
with text as Postal code is a child node of the <div>
tag with class attribute as q_codePostal.
As per your code trial you have used:
WebElement detailPostCode = driver.findElement(By.xpath("//label[@for='q_codePostalDetailAU']"));
In this expression //label[@for='q_codePostalDetailAU']
will always refer to the descending <input>
tag with id
attribute as q_codePostalDetailAU. Hence your code trial didn't work.
As an alternative you can use the following solution:
WebElement detailPostCode = driver.findElement(By.cssSelector("div.q_codePostal>label"));
Upvotes: 1
Reputation: 914
Can you try this xpath:
driver.findElement(By.xpath("//div[@class='q_codePostal']/label"));
Upvotes: 0