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'?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With