tebeka / selenium

Selenium/Webdriver client for Go
MIT License
2.53k stars 409 forks source link

having trouble with submit() and click() functions #116

Open turkogluc opened 6 years ago

turkogluc commented 6 years ago

I am trying to send a login form. When form have a <input type="submit"> finding the element and using elem.Click() works. However with the following form which includes a button instead input tag:

<form action="/bwapp/login.php" method="POST">
        <p><label for="login">Login:</label><br />
        <input type="text" id="login" name="login" size="20" autocomplete="off"></p> 
        <p><label for="password">Password:</label><br />
        <input type="password" id="password" name="password" size="20" autocomplete="off"></p>
        <p><label for="security_level">Set the security level:</label><br />
        <select name="security_level">
            <option value="0">low</option>
            <option value="1">medium</option>
            <option value="2">high</option>
        </select>
        </p>
        <button type="submit" name="form" value="submit">Login</button>
    </form>

And here is my code

func (cd *chromeDriver) login(url string){
    // Navigate to the simple playground interface.
    if err := cd.webDriver.Get(url); err != nil {
        log.Println("PANIC:",err)
    }

    // Find all Forms
    forms, err := cd.webDriver.FindElements(selenium.ByXPATH,"//form")
    if err != nil {
        panic(err)
    }

    var text string
    for _,form := range forms{
        inputs,err := form.FindElements(selenium.ByXPATH,"//input")
        if err != nil {
            log.Println("input could not be found",err)
        }
        for _,input := range inputs{
            InputName,err := input.GetAttribute("name")
            if err != nil {
                log.Println("input name could not be found",err)
            }
            InputType,err := input.GetAttribute("type")
            if err != nil {
                log.Println("input type could not be found",err)
            }
            if InputType == "text" || InputType == "password"{
                fmt.Print(InputName,":")
                fmt.Scanln(&text)
                input.SendKeys(text)
            }
        }

        button, err := form.FindElement(selenium.ByTagName,"button")

        if button == nil{
            button, err = form.FindElement(selenium.ByXPATH,"//input[@type='submit']")
            if err != nil {
                log.Println(err)
            }
            err = button.Click()
            if err != nil {
                log.Println(err)
            }
        }else{
            s,_ := button.Text()
            fmt.Println(s)

            //err = button.Click()
            err = button.Submit()
            if err != nil {
                log.Println(err)
            }
        }
}}

for this case when I face with a

owen-gxz commented 5 years ago

I have the same problem.