2017-10-07 9 views
-1

これは私のサンプルのHTMLコードです。クロムウェブドライバでカスタムタグ内の要素を取得する方法

<div class="content"> 
<M class="mclass"> 
    <section id="sideA"> 
     <div id="mainContent"> 
      <div class="requestClass"> 
       <span>Check</span> 
       <input type="text" id="box"> 
      </div> 
     </div> 
    <section> 
    <section id="sideB"> 
     ... 
    <section> 
</M> 
</div> 

テキストフィールド(「ボックス」)に値を設定したいとします。 、私のXpath IDが正しいコード

driver.findElement(By.xpath("...")).sendKeys("SetValue"); 

以下のように、それはページ内に存在していますが、私はので、私のカスタムタグのこのエラーを取得していますなぜ、このエラーに

no such element: Unable to locate element: {"method":"xpath","selector":"id("..."} 

を取得しています設定するだから私は疲れましたはいの場合、カスタムタグ内の要素を取得する方法は?

+1

は、あなたの完全なXPathを含んでいます。あなたがIDを使用していない理由... – Grasshopper

+1

Idを使って試してみましたか? – amitbobade

+2

なぜ 'id'を使わないのですか?ちょうど: 'driver.findElement(By.id(" box ")。sendKeys(" SetValue ");' – krokodilko

答えて

0

XPathを使いたい場合は、これは、あなたが持っている私の提案、私はCssSelectorも

driver.FindElement(By.CssSelector(@"m[class='mclass'] input")).SendKeys("AB"); 
0

をWORKS-として、カスタムタグは、すべての問題の原因あなたはそれを見つけることIDまたはxpathを使用することができるとは思わないME-

driver.FindElement(By.XPath(@"//*[@id='box']")).SendKeys("AB‌​"); 

のために働いていましたIDを使用します。明示的に要素を表示するには、明示的に待機します。

IDを使用して、あなたのコードは次のようである:

 WebElement elem= driver.findElement(By.id("box")); 
     WebDriverWait wait=new WebDriverWait(driver, 10); 
     wait.until(ExpectedConditions.visibilityOf(elem)); 
     elem.sendKeys("test"); 

また、あなたはに代表されるテキストフィールドに何らかの値を埋めるために提供してきたHTMLを1としてJavascriptExecutor

 WebElement elem= driver.findElement(By.id("box")); 
     WebDriverWait wait=new WebDriverWait(driver, 10); 
     wait.until(ExpectedConditions.visibilityOf(elem)); 
     JavascriptExecutor myExecutor = ((JavascriptExecutor) driver); 
     myExecutor.executeScript("arguments[0].value='test';", elem); 
0

を使用することができます<input type="text" id="box">次のコード行のいずれかを使用できます。

  1. cssSelector

    driver.findElement(By.cssSelector("section#sideA input#box")).sendKeys("SetValue"); 
    
  2. xpath

    driver.findElement(By.xpath("//section[@id='sideA']//input[@id='box']")).sendKeys("SetValue"); 
    
関連する問題