2014-11-04 3 views
5

저는 장시간 벽에 머리를 부딪 히고 있습니다. 그래서 "전문가"에게 아래의 코드가 작동하지 않는 이유를 묻습니다 (암호 입력) PhantomJS와 함께하지만 Firefox에서 잘 작동합니다. 가장 혼란스러운 것은 하나의 필드 항목 (사용자 이름)이 성공했지만 두 번째 항목은 전혀 작동하지 않는다는 것입니다. 페이지가 잘로드되고 다른 구성 요소가 제대로로드되었는지 테스트 코드가 포함되어 있습니다. 아래WebDriver PhantomJS 요소를 찾을 수 없습니다. 그러나 Firefox에서 제대로 작동합니다.

참조 코드 : WebDriver 내 경험에서

import java.io.File; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.phantomjs.PhantomJSDriver; 

public class login { 

public static void main(String[] args) { 
    WebDriver driver; 
    Boolean verbose = false; //Change to true to test it with firefox 
    String phantomPath = "../phantomjs-1.9.8-linux-i686/bin/phantomjs"; 
    String url = "https://www.britishairways.com/travel/redeem/execclub/_gf/en_us"; 

    if (verbose) { 
     driver = new FirefoxDriver(); 
     } 
    else{ 
     File file = new File(phantomPath); 
     String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8"; 
     System.setProperty("phantomjs.binary.path", file.getAbsolutePath()); 
     System.setProperty("phantomjs.page.settings.userAgent", userAgent); 

     driver = new PhantomJSDriver(); 
     } 
    driver.get(url); 
    try{ 
     driver.findElement(By.id("membershipNumber")).sendKeys("1234"); 
     System.out.println("ID input successful"); 
     if (driver.findElement(By.id("ecuserlogbutton")).isDisplayed()) { 
      System.out.println("Login Button is present"); 
     } 
     //This is where it fails with PhantomJS but work with Firefox 
     driver.findElement(By.cssSelector("#pintr > #password")).sendKeys("1234");   
     System.out.println("password input successful"); 
     } 
    catch (Exception e){ 
     System.out.print(e.getMessage()); 
     } 
    driver.close(); 
} 
} 
+0

타이밍 문제 일 수 있습니다. 각 findElement 앞에 Thread.Sleep (2000)을 사용하고 동작을 관찰하십시오. 작동하는 경우 타이밍 문제라는 것을 알고 있습니다. 또한 WaitForPagetoLoad라는 메서드가 있습니다. 요소에 입력하기 전에 호출 할 수 있습니다. – neo

+0

글쎄, 내 자신의 문제를 해결. CSS 선택기가 PhantomJS에서 작동하지 않는 것처럼 보입니다. by.xpath를 .//*[@id='password '와 함께 사용하고 이제는 작동합니다. – ucipass

+0

네오 감사합니다. 실제로 코드를 일식과 함께 매우 천천히 디버깅 해 보았습니다. 아직도 CSS 선택기가 작동하지 않는 이유는 확실하지 않습니다. – ucipass

답변

8

PhantomJS 1.x는 요소 ID에 문제가 있습니다. 페이지에있는 두 개의 요소에 대해 password을 사용하기 때문에 사이트가 손상되었습니다. selector의 id를 요소 유형 (input)으로 바꾸기 만하면됩니다.

driver.findElement(By.cssSelector("#pintr > input")).sendKeys("1234"); 
+0

당신은 선생님입니다! 나는 당신에게 엄지 손가락을주기위한 명성이 없지만, 많이 감사합니다! 나는 암호의 중복 된 이름을 알아 차리지 못했다. – ucipass

+0

내 대답에 [수락] (http://meta.stackexchange.com/a/5235/266187) 할 수 있습니다. 또한 페이지에서 어떤 일이 일어나는지 스크린 샷을 통해 확인하는 것이 좋습니다. –

+0

네, 고마워요! 페이지를보기 위해 화면 캡처를 사용했지만 빈 필드 만 보았습니다. 다음 번에 중복을 찾으려면 요소 ID를 찾아야 할 것입니다. 도와 주셔서 다시 한번 감사드립니다 !!! – ucipass

1

Try the methods from this link

, 그것은 보통 타이밍 문제입니다. 위의 링크에서 코드 시작 부분에있는 메서드를 호출하면 코드를 찾기 전에 모든 내용이로드되는지 확인할 수 있습니다. 또는 요소를 찾기 전에 충분한 시간 동안 간단하게 Thread.Sleep을 사용할 수 있습니다.

관련 문제