2015-12-27 26 views
1

我想找到硒2與C#標籤,但我不能訪問與ID或XPath或等div標籤這是我的代碼:如何在Selenium 2 WebDriver中選擇div ID?

driver.FindElement(By.Id("captcha")); 

和網頁源:

<body id="mainBody"> 
<div class="top_bar" style="min-height:210px"> 
    <div class="m_sidebar_view"> 
     <div id="captcha" style="position: relative; float: right; margin: 6px 35px 0 0; overflow: auto;"><div/> 
    </div> 
</div> 
</body> 
+0

試試這個XPath:切換到它搜索的驗證碼元素之前「// DIV [@類='top_bar 」]/DIV/DIV「 –

+0

你用它想要什麼 –

回答

0

雖然目前還不清楚您的問題是什麼,但最常見的錯誤之一併不是等待元素。因此,嘗試to wait

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
IWebElement myDynamicElement = wait.Until<IWebElement>((d) => 
{ 
    return d.FindElement(By.Id("captcha")); 
}); 
+0

感謝UR?答案兄弟,但我確定我的問題沒有等待,網絡驅動程序無法找到具有此ID的元素,我不知道爲什麼 –

1

驗證碼經常裝載的iframe內(見,例如,google recaptcha demo源代碼)。

driver.SwitchTo().Frame("frame_name_or_id"); 
相關問題