0

我对硒有一些问题。

我想String content用 id="source" 向我的网络上的一个元素发送一些内容,并在此处提交是我的代码

driver.get("http://hiriji.com/");
        WebElement viet = driver.findElement(By.id("source"));
        viet.sendKeys(content);
        viet.submit();

它运作良好,直到内容太大。

viet.sendKeys(content);

网页突然变成 404 错误页面。所以它在

viet.submit();

我的问题是如何忽略这个错误并继续下面的代码。我不需要将所有内容都发送到网络上,如果它太大可以忽略...

堆栈跟踪说:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 392 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_31'
Driver info: driver.version: RemoteWebDriver
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:244)
    at org.openqa.selenium.remote.RemoteWebElement.submit(RemoteWebElement.java:81)
    at RecieveData.TranslateStatus.translateViToEn(TranslateStatus.java:22)
    at RecieveData.htmlParser.parseHtml(htmlParser.java:43)
    at RecieveData.AddressToHtml.parseHtmlStatus(AddressToHtml.java:105)
    at RecieveData.MainProcess.main(MainProcess.java:29)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element not found in the cache - perhaps the page has changed since it was looked up
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_31'
Driver info: driver.version: unknown
4

2 回答 2

0

可能原因:手术后

driver.get("http://hiriji.com/");

您没有得到应该在页面上加载的所有元素。添加隐式等待 driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);以验证我是否正确

driver.get("http://hiriji.com/");
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
 WebElement viet = driver.findElement(By.id("source"));
        viet.clear();
        viet.sendKeys(content);
        viet.submit();//or as alternative try to click on 'submit' button
于 2012-10-24T13:02:45.987 回答
0

只需检查内容的长度。如果它的长度超出您的限制,请不要让表单提交。

if(content.length()<=acceptableLimit)
viet.sendKeys(content);
    viet.submit();
于 2012-10-24T10:06:21.710 回答