2017-10-07 13 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> 

텍스트 필드 ("상자")에 일부 값을 설정하고 싶습니다. 그래서 내가 피곤

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

내 XPath는 ID가 정확한지,이 페이지에 존재하는 것하지만이 오류 때문에 내가 내 사용자 정의 태그의이 오류를 얻고있다 왜

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

을 얻고 코드를 아래와 같이 설정합니다, 그렇다면 맞춤 태그 안에 요소를 가져 오는 방법은 무엇입니까?

+1

는 완전한 XPath를 포함한다. 이드를 사용하지 않는 이유는 무엇입니까 ... – Grasshopper

+1

이드를 사용해 보았습니까? – amitbobade

+2

왜'id'를 사용하지 않습니까? 그냥 :'driver.findElement (By.id ("box"). sendKeys ("SetValue");' – krokodilko

답변

0

XPath를 계속 사용하려는 경우 이것은 당신이 나의 제안을 가지고, 나는 CssSelector 또한 당신은 ID 또는 xpath 그것을 찾는 데 사용할 수 있습니다

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

을 작품 -로 정의 태그가 어떤 문제를 일으키는 생각하지 않는다 ME-

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

근무 ID을 사용하십시오. 또한 명시 적 요소가 표시 될 때까지 기다립니다. ID를 사용

, 당신의 코드는 다음과 같다 다음 HTML이가 표현하는 텍스트 필드에 어떤 값을 채우기 위해 제공 한 당으로

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

또한 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");