0

I have a particular keyword(displayable, not a part of attributes) in a webpage for which I need to know the font-style, font colour and background colour, among other properties. I am using php, and the three options that I have are:-

  1. Find the hierarchy of the element in which the keyword exists and then combine the inline styles, various css files etc. Too much work and feels like reinventing the wheel.
  2. Recurse through the xpath to find the keyword, and then use a webdriver to find the properties.
  3. Use a web-driver to directly find the keyword in the text using some inbuilt function and then find the properties.

Can somebody please offer suggestions/pros/cons for the alternatives present? If your choice is (3), which webdriver would you prefer to use and what is the function that you would use?

goluhaque
  • 561
  • 5
  • 17

1 Answers1

1

Essentially the strategy should be,

  1. Use xpath contains to find all elements who have 'displayable' in the text
  2. And access css attributes for each of the element

I don't know php, but in Java you could

List<WebElement> elements = driver.findElements(By.xpath("//*[text()[contains(.,'displayable')]]"));
for(WebElement element : elements) {
  System.out.println(element.getCssValue("font-style"));
}

php code should be something like below, but I can't vouch for it since I don't know it well. Reference here

$elements = $driver->findElements(WebDriverBy::xpath("//*[text()[contains(.,'displayable')]]"));
foreach($elements as $e)
{
    echo $e->getCSSValue('font-style');
}

If you are interested in knowing how this xpath works, check an awesome answer here

Community
  • 1
  • 1
nilesh
  • 14,131
  • 7
  • 65
  • 79
  • I have a doubt in general. Assume the body text as `
    abc
    keyword
    def
    keyword
    `, and I need to find the keyword "keyword". As you can see, the immediate parent elements of both occurrences is just "div", but the divs will inherit different properties due to different hierarchies. Does the driver differentiate between that when returning the css properties? I am not very well-versed with web automation, xpath and web drivers, so apologies if my understanding of the same is wrong.
    – goluhaque Feb 15 '16 at 21:01
  • Technically it should since those are different individual elements with different css attributes. The _only_ thing common in them is the text – nilesh Feb 15 '16 at 21:17
  • Thanks. Trying your solution out. – goluhaque Feb 15 '16 at 21:18
  • I just tried something like this `
    keyword
    keyword
    ` and it only returned 1 match. I checked it and it seems that whenever a child element contains a match, it does not check whether parent elements (or any ancestors) have a subsequent match. Any ideas why this happens?
    – goluhaque Feb 15 '16 at 22:41
  • Ah, you are right. Xpath was incorrect, we should use `//*[text()[contains(.,'keyword')]]`. I updated the answer, can you try now? More details on the xpath pattern can be found [here](http://stackoverflow.com/questions/3655549/xpath-containstext-some-string-doesnt-work-when-used-with-node-with-more) – nilesh Feb 16 '16 at 00:54