简体   繁体   中英

OpenQA.Selenium.NoSuchElementException: 'Unable to locate element'

I'm trying to find and click on an element using the pages xpath. When I click on the specific element and inspect it using firebug the path never works and does not seem to be found even though firebug is able to find both the relative path and the absolute path?

IWebDriver driver = new FirefoxDriver();
   driver.FindElement(By.XPath(
            "html/body/div[5]/div/div[103]/div[2]/div/div[1]/div[5]/div/div[3]/div[2]/div/img[2]")).Click();

and here is the relative path that firebug identifies

  .//*[@id='16']/div[2]/div/div[1]/div[5]/div/div[3]/div[2]/div/img[2]


  <img class="Qv_CellIcon_right Qv_ULC" alt="" style="cursor: pointer; max-width: 202px; max-height: 16px;" src="/QvAjaxZfc/QvsViewClient.aspx?datamode=binary&name=ULC&host=QVS%40fuzionpulse-tst&slot=&public=only&xrfkey=nX73wyD2jq281aHj"/>

Extremely verbose XPaths run the risk of not being located, because of how exact the HTML has to be structured in order to "match".

Looking at the sample XPath/HTML you've provided, I see an id located in a parent element and class attributes in the <img> , which I would use to construct a more simplified XPath:

//*[@id='16']//img[@class='Qv_CellIcon_right Qv_ULC']

Explanation:

  • //*[@id='16'] - Select first matching node, where id equals '16'
  • //img[@class='Qv_CellIcon_right Qv_ULC'] - Select first matching <img> , where class attributes equals 'Qv_CellIcon_right Qv_ULC'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM