Adrija
Adrija

Reputation: 99

Finding Xpath Selenium

I have the following element:

<div class="PickList visible" 
 widgetid="Palette" id="Palette">
 <span class="stuff">
 <span class="stuff"><span class="lbl">A-B</span><span class="no">1111</span> 
 </span>
 <span class="stuffSelect"><span class="lblSelect">C</span><span 
  class="plu">2222</span></span>

The xpath that I am using is:

Driver.driver.findElement(By.xpath("//*[@id="Palette"]//span//span[2]//span[contains(text(),'C')]"));

It's still not able to pickup the letter 'C'.

Any suggestions appreciated. Thanks.

Upvotes: 0

Views: 58

Answers (4)

supputuri
supputuri

Reputation: 14145

Here is the xpath. Consider the class visible in your xpath or css. As this denotes that this div might be not visible sometimes. So always be sure to use the visible if you have it as part of the class.

CSS

div.PickList.visible span.lblSelect

xpath

//div[@class='PickList visible']//span[@class='lblSelect']

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Please try the below Xpath.It will print you 'C'

driver.findElement(By.xpath("(//div[@id='Palette']//span[1]//span[2]/span)[1]")).getText()

Upvotes: 0

Govardhan Sriramdasu
Govardhan Sriramdasu

Reputation: 56

you can try below xpath to track from div.

driver.findElement("//*[@id='Palette']/span[2][@class='stuffSelect']/span[1][contains(text(), 'C')]");

Upvotes: 1

Sameer Arora
Sameer Arora

Reputation: 4507

xpath you are using is incorrect. I am providing you with the correct xpath or you can directly fetch it using the className as well.

Updated xpath as per the discussion:

WebElement selectedCharacter = driver.findElement(By.xpath("//div[@id='Palette']//span[@class='lblSelect']"));
selectedCharacter.getText();

By using className:

WebElement selectedCharacter = driver.findElement(By.className("lblSelect"));
selectedCharacter.getText();

Upvotes: 0

Related Questions