Appi
Appi

Reputation: 95

How do I extract the title attribute and the text using xpath or css selector?

From the below code i would like to fetch the text "Manager". The first and third line contains the word "Manager".

<a id="HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_a" class="level2" treenode_a="" onclick="" target="_blank" style="" title="Manager" xpath="1">
<span id="HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_ico" title="" treenode_ico="" class="button ico_close" style="width:0px;height:0px;"></span>
<span id="HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_span" class="node_name">Manager</span>
</a>

I tried with the following 3 xpath but none of it is giving the text "Manager".

(xpath ="//span[@id='HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_span']/text())
(xpath ="//*[@id=\"HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_a\"]")
(xpath ="//span[@id='HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_span']")

Also would like to fetch the word "Employees" from the following code.

<input id="HieararchyDropdownMenu" class="selectedHierarchyValue" type="text" readonly="" value="Employees" xpath="1">

Any help would be appreciated.

Upvotes: 0

Views: 1985

Answers (4)

Alok kumar
Alok kumar

Reputation: 11

try using

//span[contains(text(),"Manager")]

driver.findElement(By.xpath("//input[@id='HieararchyDropdownMenu']).getAttribute("value")

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193088

To extract the title Manager and text Manager, you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Title Manager:

    • Using cssSelector:

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.level2[id^='HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_']"))).getAttribute("title"));
      
    • Using xpath:

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='level2' and starts-with(@id, 'HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_')]"))).getAttribute("title"));
      
  • Text Manager:

    • Using cssSelector and getText():

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.level2[id^='HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_'] span.node_name[id^='HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_']"))).getText());
      
    • Using xpath and getAttribute():

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='level2' and starts-with(@id, 'HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_')]//span[@class='node_name' and starts-with(@id, 'HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_')]"))).getAttribute("innerHTML"));
      

Upvotes: 0

JNo
JNo

Reputation: 9

As mentioned above you can follow the printed code to run it in your IDE.

In order for you to locate the element in the DOM you can try the below.

CSS seclector:

To fetch the text manager from the title :

$$('[id="HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_a"]').pop().getAttribute("title");

To fetch the content manager :

$$('[id="HieararchyDropdownTreePCPPerformanceBusinessUnitIdHierarchyDropdownWidget_3_span"]').pop().textContent;

To fetch the text Employees:

CSS seclector:

$$('[id="HieararchyDropdownMenu"]').pop().getAttribute("value");

Upvotes: 0

KunduK
KunduK

Reputation: 33384

To get the title Manager Try this.

String title=driver.findElement(By.xpath("//a[@class='level2']")).getAttribute("title");
System.out.println(title);

To get the text manager Try the following xpath.

String text=driver.findElement(By.xpath("//a[@class='level2' and contains(@title , 'Manager')]//span[@class='node_name']")).getText();
System.out.println(text);

To Get the value Employees you need to use getAttribute("attributename")

String idval=driver.findElement(By.id("HieararchyDropdownMenu")).getAttribute("value");
System.out.println(idval);

Upvotes: 2

Related Questions