Abhishek Kalotra
Abhishek Kalotra

Reputation: 139

Unable to locate element method XPath , Selector

I am not sure why its not able to identify the control holding the value. I tried with Class as well which did not work. Could any one please let me know what should a do.

this is my code

    string locator = string.Format("//*[@class='getlist']/div");
    Random elenumber = new Random();
    int num = elenumber.Next(1, 10);
    IWebElement fav = driver.FindElement(By.XPath(locator + "[" + num + "]" + "/div[@class='col-xs-12 col-md-4 col-sm-4 left-hm-contactus-hm']/div/div[@class='col-md-6 fav_hm']/center/form/input[@name='add']"));
    Console.WriteLine(fav);
    fav.Click();
    Thread.Sleep(1000);

this is the error i'm getting

OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='getlist']/div[1]/div[@class='col-xs-12 col-md-4 col-sm-4 left-hm-contactus-hm']/div/div[@class='col-md-6 fav_hm']/center/form/input[@name='add']"}

Upvotes: 1

Views: 1134

Answers (2)

Said Yusifli
Said Yusifli

Reputation: 190

try to use this code.

 String myXpath = "//*[@class='getlist']/div[" + num + "]/div[@class='col-xs-12 col-md-4 col-sm-4 left-hm-contactus-hm']/div/div[@class='col-md-6 fav_hm']/center/form/input[@name='add']";
    IWebElement fav = driver.findElement(By.xpath(myXpath));

and also this is very big and bad xpath. share the web url and web element name which you are trying to use maybe i can give better xpath which will use random numbers from 1 to 10 and each time will gave random element.

Upvotes: 0

Alichino
Alichino

Reputation: 1736

I can think of two possibilities:

  1. The element is located within an iframe, in which you need to access that iframe first with driver.SwitchTo().Frame(frame)

  2. div[@class='col-md-6 fav_hm'] <-- that is a compound class. From what I remember webdriver doesn't handle compound classes. I would advise changing that to div[contains(@class, 'col-md-6') and contains(@class, 'fav_hm')] same with all other compound classes.

Upvotes: 1

Related Questions