2016-08-05 64 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));