2016-08-05 62 views
0

我想識別的元素,但無論我使用哪種搜索策略,它仍然會拋出一個未發現的異常元素 我不確定是否因爲隱藏的javascript。 請幫我識別與名QuotationIDTextWebElement沒有識別

下面的元素是代碼

<table onmousedown="javascript:hide();" id="Table5" cellspacing="0" cellpadding="0" 
       width="100%"> 
       <tr> 
        <td class="frmArea" style="WIDTH: 527px" width="527"> 
         <table class="frmcontbl" id="Table6" cellspacing="0" cellpadding="0"> 
          <tr> 
           <td style="WIDTH: 203px; HEIGHT: 28px"> 
            <span id="QuotationIDLabel" class="fieldheader" align="left">Quotation ID:</span><br> 
            <input name="QuotationIDText" type="text" id="QuotationIDText" class="input wildcard" style="WIDTH: 195px" tabindex="1" maxlength="50" size="22" /> 
+0

你嘗試過這麼遠嗎? 「frame」中的元素是否在等待? – Paras

回答

0

試試這個:

driver.findElement(By.xpath(".//*[@id='QuotationIDText']")); 
0

免責聲明:您沒有提供一個鏈接到的網頁,也沒有你嘗試過的代碼,所以我只能猜測它有什麼問題。

首先,確定您的表是不是異步加載。

要測試它,請嘗試在沒有JavaScript的情況下運行該頁面。打開Chrome或您使用的任何瀏覽器,並停用JavaScript。如果不加載JavaScript,請嘗試使用Wait

如果沒有JavaScript加載,請嘗試複製XPath

要在Chrome上執行此操作,請右鍵點擊您需要的元素,然後點擊「檢查元素」。然後,右鍵單擊所需元素,轉到「複製>」,然後選擇「複製XPath」。然後像使用@ Vikrant的答案一樣使用XPath。

0

您應該提供有關您的問題的更多信息。 但是,如果你認爲元素沒有找到,因爲一些js代碼運行不完整,你可以使用某種等待。

最準確的選擇是使用類似的東西:

public void WaitForPageLoaded(IWebDriver webDriver) 
{ 
    WebDriverWait wait = new WebDriverWait(webDriver, Timeout); 
    wait.IgnoreExceptionTypes(typeof(WebDriverTimeoutException)); 
    wait.Until(JQueryIsLoaded()); 
    wait.Until(DomIsReady()); 
} 

protected Func<IWebDriver, bool> JQueryIsLoaded() 
    { 
     return webDriver => 
     { 
     try 
     { 
      return EvalScript<long>(webDriver, "return jQuery.active").Equals(0); 
     } 
     catch (Exception) 
     { 
      return true; 
     } 
    }; 
} 

protected Func<IWebDriver, bool> DomIsReady() 
{ 
    return webDriver => EvalScript<string>(webDriver, "return document.readyState").Equals("complete"); 
} 

簡單的方法等待:

WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromMilliseconds(5000)); 
wait.IgnoreExceptionTypes(typeof(NoSuchElementException)); 
return wait.Until(driver => searchContext.FindElement(selector));