2

I want to create function that will allow to fill registration, authorization and other text forms. Something like:

def fill_form(my_list):
    forms = driver.find_elements_by_xpath(common_xpath)
    for element in forms:
        element.send_keys(my_list[forms.index(element)])

It should get as arguments a list of text values to send into <input type="text">, <input type="password"> and <textarea> elements.

So far I have:

common_xpath ='//input[@type="text" or @type="password"]'

but I can't understand how to add textarea element to this XPath to match it also

Andersson
  • 47,234
  • 13
  • 52
  • 101
  • Could you upload some HTML (or, if possible, a link to the site)? – Roberto S. May 12 '16 at 14:43
  • This applicable for all registration forms that contains Username, Password and something like Description or Comments fields... I work on non-public web-app, but I'll try to find similar – Andersson May 12 '16 at 14:45
  • @RobertoS., as a fact you can use this ticket's page :) `Search Q&A` input field in right upper corner is an `` element while if to click on `add a comment` link below you get ` – Andersson May 12 '16 at 14:56
  • I'm not entirely sure I understand your question. Are you trying to get all forms or all elements in multiple forms? If it is the first you could use xpath's | which allowed you to use multiple locators at once and returns an element if it matches to any of these locators. – RemcoW May 12 '16 at 15:00
  • is there any option to use cssSelector? – noor May 12 '16 at 15:09
  • @noor, could you provide `CSS` selector for same issue? With XPath webdriver behavior seem to be not always expected – Andersson May 13 '16 at 07:36
  • 1
    for cssSelector, u just have to add the , between the the two cssSelector – noor May 13 '16 at 08:18
  • @noor, also works. thanks for advice! – Andersson May 13 '16 at 11:04

3 Answers3

4

A simpler and more future-proof strategy would be separate the XPath expression into 2/3 distinct expressions that search for required WebElements, but if you really need to use a single XPath expression I imagine you could use the | operator to devise something along the lines of:

//input[@type="text" or @type="password"] | //textarea

Tom Trumper
  • 452
  • 2
  • 8
1

Not sure if this is going to do what you look for, but, from what I can see this Xpath could get the job done:

common_xpath = "//*[self::input[@type='text'] or self::input[@type='password'] or self::textarea]"

I don't program in Python, but tried that one in Chrome's console (using $x("...")), and it seems to do what you want. You should consider calling that XPath inside the form (path/to/your/form//*...), to make it more specific.

TIL that you could select different tags in Xpath :)

Check this related answer for more info.

Finally, as a personal note, I'm not that experience with Selenium, but I would suggest you to consider using the PageObject model design pattern, to make the tests easier to maintain. Hope it works for you.

Community
  • 1
  • 1
Roberto S.
  • 1,082
  • 1
  • 14
  • 19
0

I am not aware of Python, but this is what I would do in a JAVa

String [] commanXpath= {"text", "password"};



String xpathVar= "//input[@type='"+commanXpath[index]+"']";
System.out.println(xpathVar);
By common_xpath= By.xpath(xpathVar);

See if you can implement similar logic in Python. Can you also update original post with exact html tags.

  • this may work only for `input` elements with different types, like `text`, `password`, `checkbox`... But I need to match `textarea` element also. It is not an `input` element type, but different element – Andersson May 12 '16 at 14:36
  • You can always write xpath to match all tags . `String xpathVar= "//*[@type='"+commanXpath[index]+"']";` If you can paste html tags, I may able to help with better solution. – abhijeet kanade May 12 '16 at 14:39
  • I showed all required `HTML` tags: `input` and `textarea`. I need to use them both in single `XPath` – Andersson May 12 '16 at 14:41
  • as long as xpath for webelements have anything in common, you can write it e.g. class name, parent, any attribute value. You need to find out that common value, you can get all web elements into list and then iterate thru that list. As far as matching all tags are concerned, I have already given solution in my above comment `"//*"` This will match all tags. – abhijeet kanade May 12 '16 at 14:51
  • I need just elements that I can fill with text, but not ALL elements on page – Andersson May 12 '16 at 14:53