当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Selenium save attribute value and reuse it

问题描述:

I wonder if anyone knows how to save an attribute for an object and reuse it later when the object has changed location?

What I do first is to lokate the object on top of a grid view, click on it and press submit.

This is done by:

driver.findElement(By.cssSelector(xxxxxxxxxx)).click();

And selenium will automatically choose the top located object, unfold the object and make it possible for me to press the submit button.

I have also figured out how to get the unique identifier for that object which is by typing:

driver.findElement(By.cssSelector(xxxxxxxxx)).getAttribute("title");

Since the unique identifier is a title in this case.

So my question is how do I write a code that saves the title and clicks on it / that specific object one it has been relocated. It is very important that this code is reusable since the objects in the grid view will have different titles.

The only unique identifier I have is a title.

网友答案:

I solved the issue by creating a string with the css selector + the variable + the ending of a css selector. this means that i declared a string for the title and got it by using

String title = driver.findElement(By.cssSelector(xxxxxxxx)).getAttribute("title");
title = mydeclaredstring;

In test cas Nr 2 i created a string by

String titleString = "css[selector='" +mydeclaredstring + "']"
分享给朋友:
您可能感兴趣的文章:
随机阅读: