4

Can someone tell me an XPATH that does extract background color RGB values, or whole style, then I will remove unneeded data using Excel find/replace.

Been able to extract car color names using XPATH //div[@class='colorName']

<div class="colours" style="background-color: #040404; height: 30px; width: 130px; margin: 7px"></div>
<div class="colorName">Obsidian Black</div>

Source page: http://www.carwale.com/mercedesbenz-cars/e-class/e63amg-3049/

Future
  • 111
  • 7

1 Answers1

2

You can use the combination of substring-after() and substring-before():

substring-before(substring-after(//div[@class="colours"]/@style, "background-color: "), ";") 

Works for me in the chrome console:

> $x('substring-before(substring-after(//div[@class="colours"]/@style, "background-color: "), ";")')
"#040404"
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I extract using import.io and here your XPath DOES NOT WORK! in XPath Helper extension for Chrome it does extract only first RGB value. I need to extract RGB values of ALL car colors as does //div[@class='colorName'] – Future Jul 24 '16 at 18:01
  • @Future In understand. Unfortunately, this is the limitation of `substring-before()` and `substring-after()`, please see http://stackoverflow.com/questions/10905659/xpath-substring-before-gets-only-one-element. If you can, extract the `@style` values and post-process them separately, not in an XPath. Thanks. – alecxe Jul 24 '16 at 18:06
  • So how I can extract full style values of class="colours" ? – Future Jul 24 '16 at 18:18
  • @Future just `//div[@class="colours"]/@style` would do that. – alecxe Jul 24 '16 at 18:44
  • WORKED! Thank you, apparently is a simple XPath but somewhat it did not came in my mind at time. – Future Jul 25 '16 at 10:45