Jacopo Biondi
Jacopo Biondi

Reputation: 33

Xpath: how to find a div using the elements in the div

i writed this line of code (i know it isn't correct)

driver.find_element(By.XPATH, '//div[contains(div[contains(string(),"Example_String"))]').click()

i need to find the div that contains a div that contains a string. how to can i do that?

sorry for grammatical errors, i'm not english🥲

Upvotes: 0

Views: 54

Answers (3)

indayush
indayush

Reputation: 67

If the child div is an immediate child of parent div - //div/div[contains(text(),'SOME_TEXT')]

If the child div is somewhere far inside the parent div - //div//div[contains(text(),'SOME_TEXT')] OR //div[contains(text(),'SOME_TEXT')]

Upvotes: 0

Marek
Marek

Reputation: 448

I'm not so sure about JaSON's solutions, neither of them doesn't look like a valid xpath to me...

try this, it should work for you:

//div[div[contains(text(), 'Example_String')]]

Upvotes: 1

JaSON
JaSON

Reputation: 4869

Try this one:

'//div[contains(div,"Example_String")]

for child div or

'//div[contains(.//div,"Example_String")]

for descendant div

Upvotes: 1

Related Questions