WebDriver PhantomJS Unable to find item, but works fine with Firefox

I banged my head on the wall for a long time, so I thought I would ask the “experts” why the code below would not work (enter the password) using PhantomJS, but it works fine with Firefox. The most disturbing of all is that one field record (username) is successful, but the second does not work at all. The page loads just fine, and I included test code to verify that other components were loading.

See the code below:

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(); } } 
+5
source share
2 answers

PhantomJS 1.x has a problem with item identifiers. The site is corrupted because it uses password for two elements on the page that should never have happened. A simple change of identifier in a selector with an element type ( input ) resolves it.

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

Try the methods from this link.

From my experience with WebDriver, synchronization problems usually arise. Call the method in the link above at the beginning of your code so you can make sure everything loads before trying to find them. Or you can just use Thread.Sleep with a long enough time before searching for items.

+1
source

Source: https://habr.com/ru/post/1206184/


All Articles