Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find element by atrribute using selenium?

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.

like image 861
Yan Song Avatar asked Nov 03 '25 22:11

Yan Song


1 Answers

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.

like image 79
alecxe Avatar answered Nov 06 '25 12:11

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!