4

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
  • 2,285
  • 4
  • 18
  • 27

2 Answers2

7

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
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 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
0

Latest code for getting value of any attribute of a tag or element:

pros=driver.find_elements(By.XPATH,"//div[@data-element-name='facility-highlights']")