2017-12-27 45 views
1

텍스트 상자가 필드를 자동 완성하기위한 인텔리전스를 제공하는 테스트 사례를 자동화하려고합니다. http://demoqa.com/autocomplete/자동 완성 텍스트 상자를 자동화하는 방법

이 코드는 찾아 브라우저에서 제공하는 정보에서 옵션을 찾을 수없는 나

dr.findElement(By.id("tagss")).sendKeys("a"); 
    Thread.sleep(300); 
// dr.findElement(By.id("ui-id-53")).click(); 
    Actions act = new Actions(dr); 
    act.moveToElement(dr.findElement(By.id("ui-id-53"))).click().build().perform(); 

에 의해 작성된 코드를 찾아주세요 : 자동 완성 텍스트 상자 아래 링크를 찾아주세요. 도와주세요.

답변

2

페이지가 다시로드 된 후 해당 옵션의 HTML ID가 변경되기 때문에 DOM에 자동 제안 된 옵션 요소를 찾을 수 없습니다.

이 경우 XPath를 사용하여 요소를 식별해야합니다.

System.setProperty("webdriver.chrome.driver","C:\\WebDriver\\TestAutomation\\grid\\chromedriver.exe"); 
WebDriver driver = new ChromeDriver(); 
driver.get("http://demoqa.com/autocomplete/"); 
driver.manage().window().maximize(); 
driver.findElement(By.id("tagss")).sendKeys("a"); 
WebDriverWait wait = new WebDriverWait(driver,10); 
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("ui-id-1")))); 
WebElement javaOption = driver.findElement(By.xpath(".//li[@class='ui-menu-item' and text()='Java']")); 
javaOption.click(); 

아니고 좋은 연습이 Thread.sleep();

희망이 도움말을 사용하는 - 당신이 다음 코드가 있어야한다, 자바 자동 제안 옵션을 클릭합니다 가정하자.

+0

감사의 내가 시도했지만 내 평판 (11)과 내 평판이 그렇게하기에 충분하지 않습니다 말한다 –

+0

작업. 그 죄송합니다. –

0

런타임시 변경할 수있는 동적 ID를 사용하고 있기 때문에. 이 코드를 시도하고 나에게 모든 쿼리에 대해 알려 - 당신의 도움에 대한

public class AutoSuggest { 

    public static void main(String[] args) throws InterruptedException { 
      try { 

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ranosys\\workspace\\MyTest\\chromedriver.exe"); 
        WebDriver driver = new ChromeDriver(); 
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
        WebDriverWait wait=new WebDriverWait(driver,50); 

        driver.manage().window().maximize(); 

        driver.get("http://demoqa.com/autocomplete/"); 
        driver.findElement(By.id("tagss")).sendKeys("a"); 
        Actions act = new Actions(driver); 
        List<WebElement> lst= driver.findElements(By.xpath("//li[contains(@id,'ui-id')]")); 
        for(WebElement element:lst){ 
         element.click(); 
         break; 
        } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
    } 


} 
+0

도와 줘서 고마워. 그 일. 코드를 조금 변경했습니다. 'List 옵션 = dr.findElements (By.className ("ui-menu-item")); 대한 \t \t \t : (.. x.getText()와 toLowerCase() 등호 ("자바")) (WebElement X 옵션) \t { \t \t 경우 \t \t { \t \t \t x.click (); \t \t} \t} –