Selenium + phantomJS Web 元素内部异常



我正在使用jrel 8.0_144selenium 3.5.0phantomJS driver 2.1.1执行网页抓取。

对于 id 为"quantity"的输入元素,代码:

public boolean enterQuantity (WebDriver driver, String id, int qty)
{
boolean result = false;
long delay = 30000;   // should be ample!
try
{
WebDriverWait wait1 = new WebDriverWait (driver, delay);
wait.until (ExpectedConditions.presenceOfElementLocated (By.id (id)));
WebElement we = driver.findElement (By.id (id));
WebDriverWait wait2 = new WebDriverWait (driver, delay);
wait2.until (ExpectedConditions.visibilityOf (we));
we.sendKeys (Integer.toString (qty));
result = true;
}
catch (Exception e)
{
System.out.println ("Exception : " + e.getMessage ());
}
return result;
}

生成类似以下内容:

|.enterQuantity : we = [[PhantomJSDriver: phantomjs on XP (b2ff4fa0-7e9a-11e8-bbd0-d99690573e94)] -> id: quantity]
|.waitUntilVisible : we = [[PhantomJSDriver: phantomjs on XP (b2ff4fa0-7e9a-11e8-bbd0-d99690573e94)] -> id: quantity]
|.waitUntilVisible : Exception : Expected condition failed: waiting for visibility of [[PhantomJSDriver: phantomjs on XP (b2ff4fa0-7e9a-11e8-bbd0-d99690573e94)] -> id: quantity] (tried for 25 second(s) with 500 MILLISECONDS interval)
Build info: version: '3.5.0', revision: '8def36e068', time: '2017-08-10T23:00:22.093Z'
System info: host: 'XYZ', ip: '192.168.0.123', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.phantomjs.PhantomJSDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, handlesAlerts=false, databaseEnabled=false, version=2.1.1, platform=XP, browserConnectionEnabled=false, proxy=Proxy(direct), nativeEvents=true, acceptSslCerts=false, driverVersion=1.2.0, locationContextEnabled=false, webStorageEnabled=false, browserName=phantomjs, takesScreenshot=true, driverName=ghostdriver, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: b2ff4fa0-7e9a-11e8-bbd0-d99690573e94
| NB we.sendKeys () executed at this point
|.enterQuantity : Exception web element "quantity" not visible

第一个观察结果是,等待WebElement可见性生成的任何中断都不会到达代码中的catch (Exception e)语句。

这段代码使用Internet Explorer driver完美地工作;但应用程序需要一个无头驱动程序。

有什么想法吗?


更新:

在这里实现@cruisepandey的解决方案是错误堆栈跟踪:

来自phantomjsdriver.log:

[ERROR - 2018-07-03T11:28:18.791Z] Session [68a4d9c0-7eb3-11e8-8639-3bb49fd74d3e] - page.onError - msg: Error fetching the availability or geometry data 
[ERROR - 2018-07-03T11:28:18.791Z] Session [68a4d9c0-7eb3-11e8-8639-3bb49fd74d3e] - page.onError - stack: (anonymous function) ([web page address redacted]) 
[ERROR - 2018-07-03T11:28:19.075Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1530617299040

你可以试试这段代码:

public boolean enterQuantity (WebDriver driver, String id, int qty)
{
boolean result = false;
long delay = 30000;   // should be ample!
try
{
WebDriverWait wait = new WebDriverWait (driver, delay);
WebElement  we = wait.until (ExpectedConditions.presenceOfElementLocated (By.id(id)));
wait.until(ExpectedConditions.visibilityOf(we));
we.sendKeys(Integer.toString(qty));
result = true;
}
catch (Exception e)
{
System.out.println ("Exception : " + e.getMessage ());
}
return result;
}

您可以使用elementToBeClickable检查元素是否已启用且可见:

public boolean enterQuantity (WebDriver driver, String id, int qty)
{
boolean result = false;
long delay = 30000;   // should be ample!
try
{
WebDriverWait wait = new WebDriverWait (driver, delay);
wait.until (ExpectedConditions.elementToBeClickable(By.id (id)));
WebElement we = driver.findElement (By.id (id));
we.click(); // click on element before send keys
we.sendKeys (Integer.toString (qty));
result = true;
}
catch (Exception e)
{
System.out.println ("Exception : " + e.getMessage ());
}
return result;
}

最新更新