1

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']"}
Ajoe
  • 1,397
  • 4
  • 19
  • 48
Abhishek Kalotra
  • 139
  • 1
  • 3
  • 12
  • 1
    Try to wait for specific condition before find element: https://stackoverflow.com/questions/43203243/how-to-get-webdriver-to-wait-for-page-to-load-c-selenium-project – Yun Nov 05 '19 at 10:54
  • 2
    Post your Relevant HTML? – KunduK Nov 05 '19 at 10:55

2 Answers2

1

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.

Pankaj Devrani
  • 510
  • 1
  • 10
  • 28
Alichino
  • 1,668
  • 2
  • 16
  • 25
0

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.

Said Yusifli
  • 170
  • 11