1

I have the below code that I am attempting to extract a specific attribute from(data-id). I am new to using selenium and have been pained by this for over a day now.

To add context to this I will give you a little background into what I am trying to achieve.

I have a webpage that has a auction, the auction has an ID, all items in the auction have unique ID's but all link to the original Auction ID. I am attempting to extract the "data-id" attribute of an element however I have yet to find out how. Here is a snippet of the code I am attempting to get the id from.

<div class="dropdown open">
  <a class="dropdown-toggle form-control" href="#" data-toggle="dropdown">
  <ul class="dropdown-menu dropdown-menu-form" role="menu">
    <li id="liAuction4c42556772376a443736343d">
      <label class="checkbox">
        <input id="chkAuction4c42556772376a443736343d" class="auction" type="checkbox" data-caption="09-10-2015 10:30:00" data-id="4c42556772376a443736343d" checked="checked"/>
09-10-2015 10:30:00
      </label>
    </li>
  </ul>
</div>

I have been on many forums and searched the whole of google and not found a solution that has worked for me yet otherwise I would not be posting the question and look like a complete Rookie.

I have attempted to use .getAttribute however I have had some issues with that and the code has never compiled, I guess I have not done something correctly.

String dataID = selenium.findElement(By.xpath("//*[starts-with(@id, 'liAuction')]")).getAttribute("data-id");

When I attempted the above the "findElement" part is underlined red and I have the following message,

"The method findElement(By) is undefined for the type Selenium".

If I change the parentisis around to look like this;

String dataID = selenium.findElement(By.xpath("//*[starts-with(@id, 'liAuction')]").getAttribute("data-id"));

"findElement" is no longer underlined, however now the ".getAttribute" part is underlined red and I have the following message, "The method getAttribute(String) is undefined for the type By"

I would really appreciate some assistance with this as I am about to throw my laptop out of the window, and I don't really want to lose all of my files.

Thanks in advance

Tony

4 Answers 4

2

You can use the getAttribute method.

First find the input element, then extract the data-id:

WebElement inputElement = selenium.findElement(By.id("chkAuction4c42556772376a443736343d"));
String data-id = inputElement.getAttribute("data-id");
Sign up to request clarification or add additional context in comments.

3 Comments

Hi thank you for the quick response, I have attempted your suggestion but "driver" is underlined in red and is asking for a variable to be declared for it. Am I correct in assuming I should replace this with "selenium"? When I have replaced it I have the same problem described in my original post
Sorry if this is a basic question but I am new to this and have not any idea yet
haha, sorry, this is what is calle "selenium" in your code!, will update my answer ;-)
1

Use below xpath:-

//input[@id='chkAuction4c42556772376a443736343d']/@data-id

Then use:-

String dataID = selenium.findElement(By.xpath("//input[@id='chkAuction4c42556772376a443736343d']/@data-id")).gettext();

Full Code:-

static WebDriver driver=null;
    public static void main(String[] args) {
         driver = new FirefoxDriver();
         driver.get("URL");
         String dataID = driver.findElement(By.xpath("//input[@id='chkAuction4c42556772376a443736343d']/@data-id")).gettext();
         System.out.println(dataID);
}

Hope it will help you :)

3 Comments

Hi thank you for this, I can see how this should work, however I have the same problem still, "findElement" is underlined red and tooltip message is "The method findElement(By) is undefined for the type Selenium". this has been my headache
I have updated my answer can you please try the full code. add your needed code in between it should work
Thank you for this, Was not entirely the correct answer but pointed me to exactly what I wanted I used the following to the the data-id attribute. Your answer helped more than you know, Thank you again String dataID = selenium.getValue("//input[@id='chkAuction4c42556772376a443736343d']/@data-id");
1

Thank you for your hekp with this guys, was not expecting to get the answer quite so quickly.

I needed to use the following line to retrieve the "data-id" attribute of an element.

String dataID = selenium.getValue("//input[@id='chkAuction4c42556772376a443736343d']/@data-id");

This ended up being alot easier that initially thought, I would like to thank @Shubham Jain for help with this his suggestion pointed me to where I needed to be.

I hope this helps others in the future

Comments

0

Hi I suggest you to use the following code. It will work absolutely fine.

WebElement element = selenium.findElement(By.xpath("//input[@class='auction']"));
String dataId= element..getAttribute("data-id");

Detail

I'm identifing the object using following xpath //input[@class='auction'] and save it to an WebElement variable.

then By using getAttribute() im getting the string from data-id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.