Satish Dhanaraj
Satish Dhanaraj

Reputation: 63

How to Pass Dynamic Values in Xpath

This is my First Question in this blog.

Am working on Selenium with C# Automation and have hit a road block. I need to click on a Checkbox which is not static and the ID is also not static so i have decided to use XPATH.

XML Code:

<div id="divJqTreeAPOS" class="tree tree-checkbox" style="height: 300px; overflow-y: scroll; overflow-x: hidden; direction: ltr;">
    <ul class="ltr">
       <li id="ChassisId:10088" class="last closed" data="POWER CONNECT 2816">
          <a class="" style="" href="">
          <ins/>
          POWER CONNECT 2816 - (3)
          </a>

XPATH Generated: .//*[@id='ChassisId:10088']/a/ins The Vale ChassisId:10088 is Dynamic, Please help me in created a Method.

This is what i have tried:

public void SelectProductGroup(string ChassisID)
    {
        //.//*[@id='ChassisId:10088']/a/ins
        webDriver.FindElement(By.XPath(".//*[@id="+ChassisID+"]/a/ins"));
    }`

Upvotes: 1

Views: 5762

Answers (4)

Aru
Aru

Reputation: 134

Try this :

driver.findElement(By.xpath("//li[contains(@id, 'ChassisId:')]/a/ins"));

Upvotes: 0

Sudhir pradhan
Sudhir pradhan

Reputation: 82

if your id is keep on changing e.g. weight90, weight80,etc.

Try

FindElement(By.XPath("//*[contains(@id,"weight')]"), this will get accepted.

Upvotes: 0

Arun
Arun

Reputation: 875

You can use following XPath which will work if Id is dynamic.

//webDriver.FindElement(By.XPath("//*[contains(@id,"ChassisId')]/a/ins[contains(text(),'POWER CONNECT')]")

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167716

In XPath you need single or double quotes around string values so change webDriver.FindElement(By.XPath(".//*[@id="+ChassisID+"]/a/ins")); to webDriver.FindElement(By.XPath(".//*[@id='"+ChassisID+"']/a/ins"));

Upvotes: 1

Related Questions