gpt4 book ai didi

java - selenium + phantomJS Web 元素内部异常

转载 作者:行者123 更新时间:2023-11-30 06:06:43 26 4
gpt4 key购买 nike

我正在使用 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 驱动程序可以完美运行;但应用程序需要 headless 驱动程序。

有什么想法吗?

<小时/>

更新:

这里实现@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;
}

关于java - selenium + phantomJS Web 元素内部异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51150768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com