Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find in html where element attribute contains string selenium

I am scraping a website that has a bit of html in this format.

</p></div><div class="content "><ul class="office-list"><li><a href="javascript:void(0)" class="_office atlanta" data-slug="atlanta" data-title="Atlanta" data-address="Twilio Atlanta<br />950 East Paces Ferry Road NE, 18th Floor<br />Atlanta, GA 30326<br />" 

I have tried using some python code which is:

items = driver.find_elements_by_xpath("//*[contains(@class, 'address')]")
    for item in items:
        addresses.append(item.text)

However in this case, its not the class which contains 'address', it is data-address. How can I search any element attribute which contains 'address'?

like image 417
Jack Avatar asked Jun 30 '26 06:06

Jack


1 Answers

You can do it with name() function

items = driver.find_elements_by_xpath("//@*[contains(name(),'address')]/..")

The text you are looking for is in the attribute. Since you know only part of the name you need to use JavaScript to get it

value = driver.execute_script(
'for (index = 0; index < arguments[0].attributes.length; ++index) {
   if (arguments[0].attributes[index].name.includes("address")) {
     return arguments[0].attributes[index].value;
   }
}', element)
like image 56
Guy Avatar answered Jul 01 '26 20:07

Guy



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!