简体   繁体   English

无法在独立浏览器中找到元素

[英]Unable to find element in stand alone browser

I have tried many different formats and am unable to locate the link that I need to click using xpath.我尝试了许多不同的格式,但无法找到需要使用 xpath 单击的链接。 Please help me learn the magic of xpath...请帮助我学习xpath的魔力...

IWebElement TCE = driver.FindElement(By.XPath("//div[*/text()='Recently Visited']/child::a[@title='Time Clock Entry']"));
            TCE.Click();

The code in the webpage for the link is as follows...网页中链接的代码如下...

<div id="RecentlyVisitedWidget" class="recentlyVisitedWidget">
   <h2 id="ctl00_12_12_RecentlyVisitedLabel">Recently Visited</h2>
      <ul class="recentlyVisitedLinks">
         <li>
            <span id="ctl00_12_12_Repeater1_ctl00_link">
               <a title="Time Clock Entry" onclick="recentlyVisitedSelect('pages/VIEW/UTMEntry.aspx?USParams=PK=ESS!MenuID=2147!PageRerId=2147!ParentRerId=72','72','2147','2147', false, false, 'Time Clock Entry', true)" href="#"> Time Clock Entry</a>
            </span>
         </li>
      </ul>
</div>

I am able to accomplish this using the following code within a webBrowser Control in a form.我可以在表单中的 webBrowser 控件中使用以下代码来完成此操作。

HtmlElement link = (from HtmlElement elem in webBrowser1.Document.GetElementsByTagName("a")
                                    where elem.InnerHtml == "Time Clock Entry"
                                    select elem).ElementAt(0);
                link.InvokeMember("Click");

I am unable to find this link when trying to do this in a stand alone browser such as IE or Firefox.在 IE 或 Firefox 等独立浏览器中尝试执行此操作时,我无法找到此链接。 I have tried searching by CssSelector, linkText, Partial linkText, link, XPath, and the Id all returning a message telling me that the element could not be found.我尝试通过 CssSelector、linkText、Partial linkText、link、XPath 和 Id 进行搜索,所有这些都返回一条消息,告诉我找不到该元素。

Any ideas on why I would be able to locate this element in a webBrowser Control but not in a browser?关于为什么我可以在 webBrowser Control 中找到这个元素而不是在浏览器中找到这个元素的任何想法?

Your XPath should be something like this你的 XPath 应该是这样的

//div[@id='RecentlyVisitedWidget']//a[@title='Time Clock Entry']

or或者

//div[h2[text()='Recently Visited']]//a[@title='Time Clock Entry'] 

Does it have to be an XPath?它必须是 XPath 吗?

driver.findElement(By.cssSelector("ul.recentlyVisitedLinks a[title='Time Clock Entry']")).click();

I would avoid XPaths, when possible, because they are more error prone and slower.如果可能,我会避免使用 XPath,因为它们更容易出错且速度更慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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