find_element(By.XPATH) not working python selenium

Viewed 156

I'm trying to select the input using the text at the bottom " hello world "

<p>
"test"
<label for="q906318:1_answer" class="accesshide">
Answer
</label>
<input type="text" name="q906318:1_answer" id="q906318:1_answer" size="11" class="form-control d-inline incorrect" readonly="readonly">
<i class="icon fa fa-remove text-danger fa-fw " title="Incorrect" aria-label="Incorrect">
::before
</i> 
" hello world "
</p>

but I can't use the

driver.find_element(By.XPATH, "//p[contains(text(),'hello world')]")

Have you any suggestions ?

2 Answers

Change:

//p[contains(text(),'hello world')]

to:

//p[text()[contains(.,'hello world')]]

or, if the target may be wrapped in another element, test the p element's string value:

//p[contains(.,'hello world')]

If it's the input child of the above p elements that you actually want, simply append /input.

If the p is the only unique element you can use(though it's almost never the case), I would get to this p(with agly xpath or with findElements) and use getText to get the text of this p. Then you'll be able to see the text as Selenium sees it.

Related