-1

I'm kind of new to Selenium testing. I want to use Selenium to detect forms in any webpage and also fill them. I don't want to be restricted to a single webpage like having to inspect the page or view page source to get an id or name or Xpath.

I just want to be able to input any webpage URL, selenium should detect a form, fill it and submit. For example, a registration or login form in any webpage. Thanks.

NB: I'm working with python please.

0

3 Answers 3

3

There are no out of the box solutions, as far as I know, for your stated problem. Mainly because your problem is too broad.

Selenium can't detect "forms" unless you tell it what forms are. To do that you need to find an identifiable CSS or XPATH selector for all the elements on any webpage that could qualify as a form input field.

Furthermore, filling every possible input field in a form with acceptable input is something you can only do if you know how a webpage classifies "good" and "bad" input. Selenium can't make that choice for you.

Submitting a form can be done in multiple ways, most webpages use a "Submit" or similar button. Selenium can't automatically detect that button is and press it for you, you need to tell it where that button is on the page via a selector.

I would suggest reading a bit more on what Selenium is and what it can do. There are multiple python/selenium tutorials available. I would first start reading about the bindings available in the python implementation for Selenium if you chose python to be your scripting language of choice for your work. http://selenium-python.readthedocs.io/index.html

7
  • I actually want to build a complete test software that can test multiple sites . Commented Jul 6, 2017 at 15:08
  • First of all, no amount of code can "test" a site, but that's a much broader discussion. For reference see satisfice.com/articles/cdt-automation.pdf. Secondly, assuming what you meant was "I want to write some code that runs a set of automated checks against some list of websites I arbitrarily define", then there is no freely available plugin solution to do that. You will need to define the checks, define pass/fail criterias for them and aggregate your test results.
    – BoboDarph
    Commented Jul 6, 2017 at 15:17
  • Thanks for the reply. I actually want to build a complete test software that can test multiple sites. I want to be able to write tests that can be work for any site not just a particular website cause i'm being limited by element ID, Name etc. That's why i wanted starting from the very basic stuffs like form registration and login. I actually gained my previous knowledge of selenium from that link selenium-python.readthedocs.io/index.html. Would still appreciate your help in any way you can. Commented Jul 6, 2017 at 15:21
  • The thing that is limiting you from reaching a conclusion is your understanding of how broad your problem is. Also, your approach to solving your problem with punctual solutions that may or may not help you reach a conclusion will most likely lead you to wrong conclusions. My answers tried to help you redefine the problem to a simpler one. One that, in my opinion, could have a solution.
    – BoboDarph
    Commented Jul 6, 2017 at 15:29
  • 1
    I'm trying to help you understand the complexity of your endeavor. With enough work and enough time, one can move a mountain. Writing selectors and heuristics to detect, fill any and all possible combinations and input fields or click buttons devised by human minds is not impossible. It is just a really really lot of work. The tricky part of testing is not clicking a button or finding a field tho.
    – BoboDarph
    Commented Jul 13, 2017 at 7:01
1

Have a look at the documentation here

You probably want to do something like:

all_inputs = driver.find_elements_by_tag_name("input")
for input in all_inputs:
   input.sendKeys("my password")
1
  • Thank you. this was able to select all the inputs at once and fill them with "my password". But that's not what i want exactly...how does it fill firstname, lastname, email.....what if its a checkbox? Commented Jul 10, 2017 at 10:06
-1

Try to verify the structure and generate patterns by hierarchy:

Also you know, there is a form element that contains all elements into the form, so for example:

url : https://kenoshasmiles.com/contact

  1. go to the website and execute this code on console: Check the pattern in order to get all posibles ways to fill any form on any web page, its my approach for this solution: Father > child > child > child > child > value => match with label "Name"

let element = document.getElementsByTagName('form')                     
for(item in element){ 
    for(child in element[item].children){ 
      for(subchild in element[item].children[child].children){ 
        for( kid in element[item].children[child].children[subchild].children){ 
          //structure: form > div > div > div > div > value
          for( subkid in element[item].children[child].children[subchild].children[kid].children){ 
            // iterate over subkids childrens
            for( last in element[item].children[child].children[subchild].children[kid].children[subkid].children){               

              //element[item].children[child].children[subchild].children[kid].children[subkid].children[last].value = "James"
              //console.log(element[item].children[child].children[subchild].children[kid].children[subkid].children[last])

              //pattern name
             
              if(element[item].children[child].children[subchild].children[kid].children[subkid].innerText.includes('Name') || element[item].children[child].children[subchild].children[kid].innerText.includes('Name')){
                element[item].children[child].children[subchild].children[kid].children[subkid].children[last].value = "Jose Jose"
                console.log('Name > ', element[item].children[child].children[subchild].children[kid].children[subkid].children[last])
              }
           
              //pattern email
              if(element[item].children[child].children[subchild].children[kid].children[subkid].innerText.includes('email') || element[item].children[child].children[subchild].children[kid].innerText.includes('Email')){
                element[item].children[child].children[subchild].children[kid].children[subkid].children[last].value = "[email protected]"
                console.log('Email > ',element[item].children[child].children[subchild].children[kid].children[subkid].children[last])
              }

              //pattern Phone
              if(element[item].children[child].children[subchild].children[kid].children[subkid].innerText.includes('Phone') || element[item].children[child].children[subchild].children[kid].innerText.includes('Phone')){
                element[item].children[child].children[subchild].children[kid].children[subkid].children[last].value = "887446714"
                console.log('Email > ',element[item].children[child].children[subchild].children[kid].children[subkid].children[last])
              }


              //pattern Phone
              if(element[item].children[child].children[subchild].children[kid].children[subkid].innerText.includes('Contact Reason') || element[item].children[child].children[subchild].children[kid].innerText.includes('Contact Reason')){
                element[item].children[child].children[subchild].children[kid].children[subkid].children[last].value = "Appoinment"
                console.log('Contact Reason > ',element[item].children[child].children[subchild].children[kid].children[subkid].children[last])
              }

                 //pattern Comments
              if(element[item].children[child].children[subchild].children[kid].children[subkid].innerText.includes('Comments') || element[item].children[child].children[subchild].children[kid].innerText.includes('Comments')){
                element[item].children[child].children[subchild].children[kid].children[subkid].children[last].value = "Hi, My name is Jose."
                console.log('Comments > ',element[item].children[child].children[subchild].children[kid].children[subkid].children[last])
              }

              
          }
        }
      } 
    } 
  }  
}  

Not the answer you're looking for? Browse other questions tagged or ask your own question.