2014-11-14 58 views
0

我已经尝试通过Id,XPath和CssSelector找到元素,但没有任何运气。测试在查找元素时超时。如何使用Selenium Webdriver识别此元素

<div xmlns="http://www.w3.org/1999/xhtml" id="HyperlinkBetaSentry" 
style="top:0px;left:521px;height:24px;width:70px;cursor:pointer;text-align:center" 
class="DashboardHyperlink"><a target="_blank" 
href="https://salsa.sentry.com/SalsaDataSentry/wafForm.aspx?__sso=1234567890" style="color:#e6e7e8;vertical-align:middle;text-decoration:none">Beta Sentry</a></div> 

我曾尝试:

var title = Driver.Instance.FindElement(By.CssSelector("#HyperlinkBetaSentry")); 
var title = Driver.Instance.FindElement(By.Xpath("//*[@id='HyperlinkBetaSentry']")); 
var title = Driver.Instance.FindElement(By.Id("HyperlinkBetaSentry")); 

回答

0

我想By.Id应该是工作。通常当发生这种情况时,这是因为寻找元素的代码是在元素存在之前执行的(例如DOM未完全加载,或者一些Javascript更改了DOM或Ajax请求等)。确认。在查找元素之前在代码中设置一个断点。确保该元素确实存在,如您所期望的那样,然后遍历试图找到它的代码。有机会找到它。

有时会导致这种情况的唯一另外一件事是,您正在寻找与您的元素不同的框架。

0

有多个问题可能导致此问题。

  1. 传递点击标签

    变种标题= Driver.Instance.FindElement(By.CssSelector( “#HyperlinkBetaSentry>一个”));

  2. 元负荷时间

  3. 重复的ID

在案例2中:您可以定义一些明确的等待元素是可见的。

WebElement myDynamicElement = (new WebDriverWait(driver, 10)) 
    .until(ExpectedConditions.presenceOfElementLocated(By.id("YourID"))); 

查看here的解释。

案例3:您可能想要使用xpath或css with multiple attributes进行文本基搜索。

.//*[.='Beta Sentry'] 
相关问题