Krishna Chandra Tiwari
Krishna Chandra Tiwari

Reputation: 111

WebDriver not identifying WebElement

enter image description here

I using xpath="//div[@class='localityKewordDropDown']/descendant::div[@class='over']/span[text()='Dwarka, New']

but the element is not getting recognized. NosuchElementException is getting encountered. Could anyone help me out here.I want to click the drop down value highlighted in the image.

Upvotes: 1

Views: 88

Answers (3)

AngryAngryTester
AngryAngryTester

Reputation: 59

It's failing beacuse there is a space after the word 'New'. The following should work.

//div[@class='localityKewordDropDown']/div/div[text()='Dwarka, New ']

Or consider serarching for the element containing the text rather than matching the entire text.

//div[@class='localityKewordDropDown']/div/div[contains(.,'Dwarka, New')] 

And as cathal mentioned, you are searching for a div and not a span.

EDIT:As you request (although I don't believe there is a difference between "descendent" and "//".

//div[@class='localityKewordDropDown']/descendant::div[text()='Dwarka, New ']
//div[@class='localityKewordDropDown']/descendant::div[contains(text(),'Dwarka, New')] 

Contains is an xpath function that allows you to query on something containing a value. It can be used with attributes, nodes but it's generally most useful for finding elements containg text. The reason this is working where as your query for the exact string fails is because the element you seek is padded with a trailing space. The contains query will find the element you are seeking as it's ignoring this trailing space.

Upvotes: 3

Sneh Tripathi
Sneh Tripathi

Reputation: 430

Try this:

WebElement name = driver.findElement(By.xpath("//*[contains(text()='Dwarka, New')]"));

Upvotes: -1

Cathal
Cathal

Reputation: 1338

i dont see anyw spans around your element, try the following:

WebElement name = driver.findElement(By.xpath("//div[@id='keyword_suggest']//div[text()='Dwarka, New']"));

Upvotes: 0

Related Questions