Help using Selenium!!!

899 views
Skip to first unread message

Park Jang Soon

unread,
Aug 13, 2013, 9:47:00 PM8/13/13
to seleniu...@googlegroups.com
Hi everyone.

I'm try to navigate web page and get some data from page using Selenium.

My java code is

package org.openqa.selenium.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example  {
    public static void main(String[] args) {
        WebDriver driver = new HtmlUnitDriver();
        WebElement element = driver.findElement(By.name("srch_date"));
        element.sendKeys("2013-08-13");
        element.submit();
        System.out.println("Page title is: " + driver.getPageSource());
    }
}

I got error message error message as follows

Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot locate element used to submit form
Build info: version: '2.34.0', revision: '11cd0ef', time: '2013-08-06 17:10:22'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_01'
Driver info: driver.version: unknown
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.submitForm(HtmlUnitWebElement.java:220)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.submit(HtmlUnitWebElement.java:179)
at org.openqa.selenium.example.Example.main(Example.java:30)

I know I have to find ID of submit buttion and use click method.

But there not submit button on web page what I search web page.

I attached just web page source.

This page does not use submit button. It uses "onclick" attributes and call function.

What shouId do?

Very helpful, thank you
ubikais.html

DongXiang

unread,
Aug 15, 2013, 1:14:10 AM8/15/13
to seleniu...@googlegroups.com
You may fire a javascript "onclick" event on your element
 
  String eventScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('"
    + "click"
    + "', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('"
    + "onclick"+ "');}";
  JavascriptExecutor js = (JavascriptExecutor) webDriver;
  js.executeScript(eventScript, webElement);
 
it will fire a "onclick" event on your element and trigger your js function.
 
--david

Date: Tue, 13 Aug 2013 18:47:00 -0700
From: jspa...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Help using Selenium!!!
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/22bab2f5-534e-4f51-8ceb-7269c2886988%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--附转发的邮件--

Introduce  l  World AIS Link  l  Login
i-ARO
Create for Flight Message
- FPL
- ULP FPL
- CHG
- DLA
- CNL
- DEP
- ARR
IFR FPL List
- Out Bound List
- In Bound List
VFR FPL List
ULP FPL List
���ǻ��� �� 032 - 741 - 7181
 
��������: 137   ��������: 248,106  
Departure List
FLIGHT PLAN
   Flight Number Origination Destination
 Click search for the list of query.
 Double click record for Retrieval of Route Pre-Flight Information Bulletin.(Only for checked FPL)

Mark Collin

unread,
Aug 15, 2013, 5:10:05 AM8/15/13
to seleniu...@googlegroups.com
Try:

WebDriver driver = new HtmlUnitDriver(true);

to enable JavaScript in HTMLUnit

Park Jang Soon

unread,
Aug 15, 2013, 9:48:25 PM8/15/13
to seleniu...@googlegroups.com
I tried but I got error as follows.
8월 16, 2013 10:43:51 오전 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
경고: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://ubikais.fois.go.kr/js/common.js', but got ''.
8월 16, 2013 10:43:52 오전 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
경고: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://ubikais.fois.go.kr/js/fois.js', but got ''.
8월 16, 2013 10:43:52 오전 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
경고: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://ubikais.fois.go.kr/js/sms.js', but got ''.
8월 16, 2013 10:43:52 오전 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
경고: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://ubikais.fois.go.kr/js/top_menu.js', but got ''.
8월 16, 2013 10:43:52 오전 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
경고: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://ubikais.fois.go.kr/js/top_func.js', but got ''.
8월 16, 2013 10:43:52 오전 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
경고: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://ubikais.fois.go.kr/IBSheet/IBSheetInfo.js', but got ''.
8월 16, 2013 10:43:52 오전 com.gargoylesoftware.htmlunit.html.HtmlScript executeInlineScriptIfNeeded
경고: <script for='mySheet' ...>: no element found with id "mySheet". Ignoring.
8월 16, 2013 10:43:52 오전 com.gargoylesoftware.htmlunit.html.HtmlScript executeInlineScriptIfNeeded
경고: <script for='mySheet' ...>: no element found with id "mySheet". Ignoring.
8월 16, 2013 10:43:52 오전 com.gargoylesoftware.htmlunit.html.HtmlScript executeInlineScriptIfNeeded
경고: <script for='mySheet' ...>: no element found with id "mySheet". Ignoring.
8월 16, 2013 10:43:52 오전 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
경고: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://ubikais.fois.go.kr/js/objSheetGeneral.js', but got ''.
Exception in thread "main" org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "RgbColor" is not defined. (script in http://ubikais.fois.go.kr/biz/fpl/depo.jsp from (27, 31) to (124, 10)#43)
Build info: version: '2.34.0', revision: '11cd0ef', time: '2013-08-06 17:10:22'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_01'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:376)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:355)
at org.openqa.selenium.example.Example.main(Example.java:19)
Caused by: com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "RgbColor" is not defined. (script in http://ubikais.fois.go.kr/biz/fpl/depo.jsp from (27, 31) to (124, 10)#43)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:669)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:601)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:601)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:576)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:1005)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventHandler(EventListenersContainer.java:208)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:228)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:813)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:737)
at com.gargoylesoftware.htmlunit.html.HtmlElement$1.run(HtmlElement.java:867)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:601)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
at com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(HtmlElement.java:872)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1268)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:237)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:441)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:309)
at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPageIfPossible(BaseFrameElement.java:170)
at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPage(BaseFrameElement.java:108)
at com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames(HtmlPage.java:1892)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:207)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:441)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:309)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:374)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:366)
... 2 more
Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: ReferenceError: "RgbColor" is not defined. (script in http://ubikais.fois.go.kr/biz/fpl/depo.jsp from (27, 31) to (124, 10)#43)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3603)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3587)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3657)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.nameOrFunction(ScriptRuntime.java:1749)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2130)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1321)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:275)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3031)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:103)
at com.gargoylesoftware.htmlunit.javascript.host.EventHandler.call(EventHandler.java:81)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:594)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:654)
... 27 more

My JAVA Code is like this.

package org.openqa.selenium.example;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new HtmlUnitDriver(true);
        
         

        // And now use this to visit Google
//        driver.get("http://www.google.com");

        String eventScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('"
           + "click"
           + "', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('"
           + "onclick"+ "');}";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        WebElement element = driver.findElement(By.name("srch_date"));
        element.sendKeys("2013-08-13");
//      WebElement webElement = driver.findElement(By.name("srch_date"));
        js.executeScript(eventScript, driver);

        // Enter something to search for
        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getPageSource());
//        System.out.println("Page title is: " + cssSelector.getText());
        
    }
}

DongXiang

unread,
Aug 18, 2013, 4:34:24 AM8/18/13
to seleniu...@googlegroups.com
HI
 
js.executeScript(eventScript, driver);
 
I think you shouldn't fire javascript event on the webdriver, it should be fired on a specified element. So the second parameter should not be "driver" but your element with 'onclick" function.
 
I am not familar with HtmlUnitDriver, with IE/Chrome/Firefox, I use this way to handle some similar situation like yours, it works fine.
 
-david
 

Date: Thu, 15 Aug 2013 18:48:25 -0700
From: jspa...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Help using Selenium!!!
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages