저는 장시간 벽에 머리를 부딪 히고 있습니다. 그래서 "전문가"에게 아래의 코드가 작동하지 않는 이유를 묻습니다 (암호 입력) 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();
}
}
타이밍 문제 일 수 있습니다. 각 findElement 앞에 Thread.Sleep (2000)을 사용하고 동작을 관찰하십시오. 작동하는 경우 타이밍 문제라는 것을 알고 있습니다. 또한 WaitForPagetoLoad라는 메서드가 있습니다. 요소에 입력하기 전에 호출 할 수 있습니다. – neo
글쎄, 내 자신의 문제를 해결. CSS 선택기가 PhantomJS에서 작동하지 않는 것처럼 보입니다. by.xpath를 .//*[@id='password '와 함께 사용하고 이제는 작동합니다. – ucipass
네오 감사합니다. 실제로 코드를 일식과 함께 매우 천천히 디버깅 해 보았습니다. 아직도 CSS 선택기가 작동하지 않는 이유는 확실하지 않습니다. – ucipass