Re: How can I get the value of the title attribute of a web element?

6,110 views
Skip to first unread message

Edwolb

unread,
Sep 19, 2012, 4:30:13 PM9/19/12
to seleniu...@googlegroups.com
Are you sure you're finding the right elements?  It looks to me like your xpath in your code sample is finding a "div", and not an "a" element.  getAttribute('title') should do the job once you have the right element.  You mentioned that the only way you can fin dthe actions is to use the given xpath.  Why not something like

WebElement delete = driver.findElement(By.xpath("//a[@title='Delete']"));

Which will find the actual button you want.  

On Tuesday, 18 September 2012 10:55:54 UTC-4, Alex wrote:
Hi

I have to test a website of another company. I use selenium webdriver for all automated scripts in Java. I have problems to identify action icons within a cell of a table.

For example:

There is a table with users of the website. Within each row there is a column with possible actions:

Row 1:
  • Action 1:
    • HTML: <a title="Show" class="anzeigen" href="/users/administration/1590;jsessionid=FagMg=0"><span class="invisible">Show</span></a>
    • XPATH: /html/body/div[3]/div[2]/div[2]/table/tbody/tr/td[7]/div/a
  • Action 2:
    • HTML: <a title="Lock" class="lock" href="/users/administration/9213;jsessionid=aZpSy=0"><span class="invisible">Lock</span></a>
    • XPATH: /html/body/div[3]/div[2]/div[2]/table/tbody/tr/td[7]/div/a[2]
  • Action 3:
    • HTML: <a title="Delete" class="delete" href="/users/administration/7822;jsessionid=okcY1=0"><span class="invisible">Delete</span></a>
    • XPATH: /html/body/div[3]/div[2]/div[2]/table/tbody/tr/td[7]/div/a[3]
Row 2:
  • Action 1:
    • HTML: <a title="Show" class="show" href="/users/administration/3386;jsessionid=FagMg=0"><span class="invisible">Show</span></a>
    • XPATH: /html/body/div[3]/div[2]/div[2]/table/tbody/tr[2]/td[7]/div/a
  • Action 2:
    • HTML: <a title="Lock" class="lock" href="/users/administration/3832;jsessionid=aZpSy=0"><span class="invisible">Lock</span></a>
    • XPATH: /html/body/div[3]/div[2]/div[2]/table/tbody/tr[2]/td[7]/div/a[2]
  • Action 3:
    • HTML: <a title="Delete" class="delete" href="/users/administration/877;jsessionid=okcY1=0"><span class="invisible">Delete</span></a>
    • XPATH: /html/body/div[3]/div[2]/div[2]/table/tbody/tr[2]/td[7]/div/a[3]

The only way to identify the actions is to use the xpath:

WebDriver driver = new FirefoxDriver();
WebElement element = driver.findElement(By.xpath("/html/body/div[3]/div[2]/div/table/tbody/tr/td[2]/div"));


The problem is that I can not be sure that there are always three actions within one row. Because of this I want to check the title information of each action before I click the action. The methods getText(), getAttribute("title") always return an empty String and not the value of title.

How can I solve the problem?


Alex

Kartik Shah

unread,
Sep 20, 2012, 12:37:55 AM9/20/12
to seleniu...@googlegroups.com
If you are not able to use getAttribute method, you can try this brut force method

1. Get all the elements with tag = "a" using driver.FindElements(By.TagName("a"))
2. Iterate through the loop and get the text using element.Text
   e.g. foreach(IWebElement element in elements)
         {  
            if(element.Text == "your expected text")
             {
               dosomething;
               }

          }
3. In step 2, you will get the text/ label/ title but based on the title if you want to perform other actions, move your element index by  defined number and perform some action.
e.g. if you want to delete a row where row has text "abc"
int count = 0
int index = 3;  index of delete button. Index can be determined by identifying elements placement from the label row.
 foreach(IWebElement element in elements)
         {  

            if(element.Text == "abc")
             {
               elements[count - index].Click(); //this statement will click the delete checkbox for the text abc
               }
count++;

          }

Kartik Shah






Alex

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/3ngS3bnh9ggJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Edwolb

unread,
Sep 20, 2012, 3:26:58 PM9/20/12
to seleniu...@googlegroups.com
If every row has a delete button, another option is:

ArrayList<WebElement> deletes = driver.findElements(By.xpath("//a[@title='Delete']")); 

Which will return all the delete buttons, with each item in the list representing the different rows.

On Thursday, 20 September 2012 12:40:51 UTC-4, Alex wrote:
The xpath I mentioned before definitely works and it gets the "a" element. I can only use your solution if there is only one element with title "Delete". The problem is that in every row are possible actions where the names of the titles are not unique. That means row 1 of the table can have an action with title "Delete" and row 2 of the table can have an action with title "Delete"...

Peter Gale

unread,
Sep 21, 2012, 3:33:02 AM9/21/12
to Selenium Users
Alex


> I have to test a website of another company.

If you have now way of knowing whether any particular row should or should not have a delete row in it how can you possibly test this aspect of the application?

Ok, if there is a delete button in the row, you can click on it and test that there are one fewer rows, perthaps.

But should clicking the delete button delete the row, or just delete somehting else and leave the row in place, and remove the delete button?

And what if there is not a delete button on a particular row ... is that correct or not? Maybe the the item in question has been deleted already?

You need to go back to the design specification, which should have a section somwhere telling you the criteria for whether a particular row on your table should or should not have a delete button. That will be the algorithm that you need to use here, firstly to test whether a delete button should exist, or not , then to check whether it performs as expected. No one here can tell you what it is. And not knowing it is why for every suggestion you have received you come back saying ... "Yes, but ..."

If you are not writing your tests based on the design specification, you are not testing it, you are just automating it, which is a different thing, and not, it seems what you want to do.

Peter


Date: Fri, 21 Sep 2012 00:09:46 -0700
From: erwing...@googlemail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: How can I get the value of the title attribute of a web element?

That is the point. Like I said before I can not be sure that every row has e.g. a delete button. There are users which I can not delete. Next I will try the brute force method of Kart.

I actually did not expect that finding a solution would be so difficult...
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/o8xARKNqKBEJ.
Reply all
Reply to author
Forward
0 new messages