3

I want to select text from the following html line:

<h3 data-reacid ='64'> text to select <h3>

I tried to use xpath. But for some reason, selenium can not find this element by xpath. I want to select the text by the 'data-reactid' attribute. I am not sure how to do this. I am using selenium with python.

Yan Song
  • 1,965
  • 4
  • 17
  • 25

1 Answers1

5

Assuming 64 is a stable value and does not change for this particular element and the attribute name is data-reactid (and not the data-reacid - looks like a typo):

driver.find_element_by_xpath("//h3[@data-reactid = '64']").text

Equivalent CSS selector based solution:

driver.find_element_by_css_selector("h3[data-reactid=64]").text

Of course, you need to account for the possible timing issues and other situations like an element being inside an iframe. This is specific to your particular situation.

alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
  • Thanks. I think that the answer might have a typo. find_element_by_xpath("//h3[@data-reactid = '64']" should be data-reactid =64. But it worked ! Can you explain why it doesnt't work when I use the xpath generated from the firefox inspect element option? – Yan Song Dec 24 '17 at 04:06