Webdriver help matching title inside an img tag

557 views
Skip to first unread message

Mike Brown

unread,
Jan 21, 2016, 11:41:12 PM1/21/16
to Selenium Users
Hi,

I'm attempting to use webdriver to write a HomeBridge plugin for my Scout alarm system. I'm having some issues trying to figure out how to find the current state, and also set the alarm to a certain state because the webpage code is crap and doesn't use and id's.

Below is the code from the site I'm trying to find. What I want to do is find the title in the img tag as that is the only thing that tells me that status of the system.

I've tried multiple combinations but nothing I come up with works, any ideas?
<ul class="recipe-list">

           
<li class=“armed” title="Edit Sleep mode">
             
<span>Sleep</span> <img src="/img/recipes/armed.png" alt="armed" title="Disarm Sleep mode">
           
</li>

           
<li class="disarmed" title="Edit Away mode">
             
<span>Away</span> <img src="/img/recipes/disarmed.png" alt="disarmed" title="Arm Away mode">
           
</li>

           
<li class="disarmed" title="Edit Mike Working mode">
             
<span>Mike Working</span> <img src="/img/recipes/disarmed.png" alt="disarmed" title="Arm Mike Working mode">
           
</li>

       
</ul>


Thanks,
Mike 

Mark Collin

unread,
Jan 22, 2016, 3:43:49 AM1/22/16
to Selenium Users
Probably easiest with an XPath like this:

//ul[@class=['recipe-list']/li[@title='Edit Sleep mode']/img

That will find the <ul> element with the class "recipe-list" and then find the <li> element below it that has the title "Edit Sleep mode" and then fin the <img> element underneath it.  If you want a different <li> element just search for a different title.  You can then take that XPath and plugin it into some code like this:

WebElement imageElement = driver.findElement(By.xpath("//ul[@class=['recipe-list']/li[@title='Edit Sleep mode']/img"))
String title = imageElement.getAttribute("title");

You will then hold the value of the image elements title in a variable called title.

saba. s

unread,
Jan 22, 2016, 4:33:12 AM1/22/16
to Selenium Users
Hi Mike,

Have you tried this. It will helpful to get the title of the image

WebElement b = driver.findElement(By.xpath("html/body/ul/li[1]/img"));
System.out.println(b.getAttribute("title"));

saba. s

unread,
Jan 22, 2016, 4:33:51 AM1/22/16
to Selenium Users
Hi Mike,

Have you tried this. It will helpful to get the title of the image

WebElement b = driver.findElement(By.xpath("html/body/ul/li[1]/img"));
System.out.println(b.getAttribute("title"));

On Friday, 22 January 2016 10:11:12 UTC+5:30, Mike Brown wrote:

murali seleniumtrainer

unread,
Jan 22, 2016, 10:45:34 AM1/22/16
to Selenium Users
Hi,

below example will helps you to get all images and retrieve titles. You can perform your actions depends up on that title values.

//collecting all img elements
        List<WebElement> imgs=driver.findElements(By.xpath("//ul[@class='recipe-list']/li/img"));
       
        //looping all images to get titles
        for(int i=0;i<imgs.size();i++){
           
            //retriving the title of all images
            String title=imgs.get(i).getAttribute("title");
           
            //just printing
            System.out.println(title);
        }

Thank You,
Murali
seleniumtrainer.com

Mike Brown

unread,
Jan 22, 2016, 1:17:15 PM1/22/16
to Selenium Users
Thanks Mark,

This looks good and using the firefox IDE I can match what I want. I'm having an issue with javascript though, I'm getting this error: InvalidSelectorError: {"errorMessage":"Unable to locate an element with the xpath expression //ul[@class='recipe-list']/li[@title='Edit Sleep mode']/img because of the following error:\nError: INVALID_EXPRESSION_ERR: DOM XPath Exception 51"

Here's that area of my script.

        imageElement = driver.findElement(By.xpath("//ul[@class='recipe-list']/li[@title='Edit Sleep mode']/img"))
         sleep = imageElement.getAttribute("title");
        
        imageElement = driver.findElement(By.xpath("//ul[@class=['recipe-list']/li[@title='Edit Away mode']/img"))
         away = imageElement.getAttribute("title");
        
        imageElement = driver.findElement(By.xpath("//ul[@class=['recipe-list']/li[@title='Edit Mike Working mode']/img"))
         stay = imageElement.getAttribute("title");
   
          if(sleep === "Disarm Sleep mode") {
            statusResult.message = "night_armed";
            statusResult.status = Characteristic.SecuritySystemCurrentState.NIGHT_ARM;
          } else if(away === "Disarm Away mode") {
            statusResult.message = "away_armed";
            statusResult.status = Characteristic.SecuritySystemCurrentState.AWAY_ARM;
          } else if(stay === "Arm Mike Working mode") {
            statusResult.message = "stay_armed";
            statusResult.status = Characteristic.SecuritySystemCurrentState.STAY_ARM;
          } else  {
            statusResult.message = "disarmed";
            statusResult.status = Characteristic.SecuritySystemCurrentState.DISARMED;
          }

Mike Brown

unread,
Jan 22, 2016, 5:05:38 PM1/22/16
to Selenium Users
Hi,

How would you do that in javascript? I've tried

//collecting all img elements
imgs = driver.findElements(By.xpath("//ul[@class='recipe-list']/li/img"))
        
        //looping all images to get titles
        for(i=0; i < imgs.size; i++)
            
            //retriving the title of all images
             title = imgs.get(i).getAttribute("title")

But get an error that title isn't defined.

Mike

murali seleniumtrainer

unread,
Jan 23, 2016, 4:32:54 AM1/23/16
to Selenium Users
Hi,

The solution what is provided is in Java.

thank you,
Murali

Mark Collin

unread,
Jan 25, 2016, 6:00:18 AM1/25/16
to Selenium Users
Which JavaScript binding are you using?

It looks like native Xpath libraries for the browser are being used, and the browser you are using doesn't like this XPath.

Mike Brown

unread,
Jan 25, 2016, 4:36:16 PM1/25/16
to Selenium Users
It's phantomjs inside of node.js.

Thanks,
Mike

Mark Collin

unread,
Jan 26, 2016, 5:02:14 AM1/26/16
to Selenium Users
What i mean is are you using this package:


Is so your best bet is to jump onto IRC (http://webchat.freenode.net/?channels=%23selenium) and have a chat with the people there.  There will be somebody who is much more familliar with the JS binding than I am.

If you are not using the above package you are not using the official selenium one and you may want to try using that instead of the one you are using.

Mike Brown

unread,
Jan 26, 2016, 2:01:58 PM1/26/16
to Selenium Users
Thanks, yes that's the package I'm using.. I'll jump into chat later, thanks for all the help!

Mike Brown

unread,
Jan 28, 2016, 3:12:29 PM1/28/16
to Selenium Users
Figured it out, it was fairly simple not sure why I kept overlooking it.. 

Instead of //ul[@class=['recipe-list']/li[@title='Edit Sleep mode']/img changing it to //ul[@class='recipe-list']/li[@title='Edit Sleep mode']/img fixed the issue (just removing the [ in front of recipe-list.. 

Thanks again!
Mike

Mark Collin

unread,
Jan 29, 2016, 8:39:50 AM1/29/16
to Selenium Users
DOH!, I missed that typo too :)
Reply all
Reply to author
Forward
0 new messages