How can I locate an element using selenium 2 that does not have unique identifiers like Name, Id

advertisements

Initiate Auto Precall (Shipper) Initiate Auto Precall (Consignee) Log Manual Precall (Shipper) Log Manual Precall (Consignee)


As per your given code in comments, Found solutions for finding element which had listed by you.

Solution 1 :

Use List of element and get it.

 List<WebElement> allOptions = driver.findElements(By.className("ui-corner-all"));

Now get element by index.

For Initiate Auto Precall (Shipper) allOptions.get(0);

For Initiate Auto Precall (Consignee) allOptions.get(1);

For Log Manual Precall (Shipper) allOptions.get(2);

For Log Manual Precall (Consignee) allOptions.get(3);

Solution 2 :

Use xpath as under given :

For Initiate Auto Precall (Shipper) (//a[@class='ui-corner-all'])[1]

For Initiate Auto Precall (Consignee) (//a[@class='ui-corner-all'])[2]

For Log Manual Precall (Shipper) (//a[@class='ui-corner-all'])[3]

For Log Manual Precall (Consignee) (//a[@class='ui-corner-all'])[4]

driver.findElement(By.xpath("<xpath>"));

Solution 3 :

Use text() and contains() functions in xpath.

For Initiate Auto Precall (Shipper)

//a[contains(text(),'Initiate') and contains(text(),'Shipper')]

For Initiate Auto Precall (Consignee)

//a[contains(text(),'Initiate') and contains(text(),'Consignee')]

For Log Manual Precall (Shipper)

//a[contains(text(),'Log') and contains(text(),'Shipper')]

For Log Manual Precall (Consignee)

//a[contains(text(),'Log') and contains(text(),'Consignee')]

driver.findElement(By.xpath("<xpath>"));

Hope you found solution.