Chandan Kumar
Chandan Kumar

Reputation: 318

How to find Xpath for a dynamic element in selenium webdriver

How to click on particular "add task" link , when more add tasks are there and I couldn't be able to find an unique Xpath

In this case the add task comes when we add new project, so every-time new add task generates.

Upvotes: 1

Views: 6125

Answers (3)

Chandan Kumar
Chandan Kumar

Reputation: 318

I used the following code and it worked perfectly fine

String ProjectName = "qrr";

driver.findElement(By.xpath("//tr/td/a[contains(text(),'"+ ProjectName +"')]/ancestor::tr[2]/td[2]/a[contains(text(),'add tasks')]")).click();

Upvotes: 0

h4k3r
h4k3r

Reputation: 91

You can use the Path but I will suggest to use the CSS Selector inlace of Path. CSS Selectors are faster than Path. To learn CSS Selector you can use http://www.w3schools.com/css/css_selectors.asp .

Upvotes: 0

alecxe
alecxe

Reputation: 474181

From what I understand, you need to locate the "add tasks" link based on the project name. The project name node is not expanded on the screenshot, but I'm assuming there is an a element there too:

String projectName = "qrr";
driver.findElement(By.xpath("//tr/td[a = '" + projectName+ "']/following-sibling::td/a[. = 'add tasks']"));

This is to locate the "add tasks" button for the qrr project name.

Or, you can locate the appropriate row and use "by link text" locator:

WebElement row = driver.findElement(By.xpath("//tr[td/a = 'qrr']"));
WebElement addTasks = row.findElement(By.linkText("add tasks"));

Upvotes: 3

Related Questions