Kafamı uzun bir süre duvara çarpıyordum, bu yüzden "uzmanlar" dan aşağıdaki kodun neden işe yaramayacağını sordum (şifre girerek) PhantomJS ile ama Firefox ile gayet iyi çalışıyor. En rahatsız edici olanı, bir alan girişinin (kullanıcı adı) başarılı olması, ancak ikincinin hiç işe yaramayacağıdır. Sayfa çok iyi yükleniyor ve diğer bileşenlerin düzgün şekilde yüklendiğini doğrulamak için test kodunu ekledim. AşağıdaWebDriver PhantomJS Öğe bulamıyor, ancak Firefox ile sorunsuz çalışıyor
bakınız kodu: WebDriver ile benim deneyim
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();
}
}
Zamanlama sorunları olabilir. Her findElement önce Thread.Sleep (2000) kullanmayı deneyin ve davranışını gözlemleyin. Eğer çalışırsa, zamanlama meselesini biliyorsunuzdur. Ayrıca WaitForPagetoLoad adında bir yöntem var. Öğelere girmeden önce bunu diyebilirsiniz. – neo
Eh, kendi problemimi çözdü. Css seçicinin PhantomJS ile çalışmaz gibi görünüyor. .xpath .//*[@id='password 'ile kullanıyorum ve şimdi çalışıyor. – ucipass
Teşekkürler neo, aslında çok yavaş yavaş kodlama ile kod hata ayıklama tarafından denedim. Hala css seçicinin neden çalışmadığından emin değilsiniz. – ucipass