-1

I am trying to get all the elements of form using selenium, but I can't seem to get it done. I need to do it dynamically without writing down the id or class of elements present in the form, The Selenium should detect the form and get all the elements name automatically that are present in the form.

The problem I'm facing is that the form is using action instead of class.

Here is the website

driver = webdriver.Chrome()
driver.get("http://stevens.ekkel.ai")
#find all form input fields via form name
content = driver.find_element_by_class_name('form')

print(content)
Khasif
  • 69
  • 1
  • 10
  • There is actually no element having a class name "form" to begin with. You need to use `driver.find_element_by_tag_name("form")`. – Dr. Regex Apr 17 '21 at 13:22
  • A link to the page is useful but the HTML could change tomorrow and then this question would be useless. You need to edit your question and add the relevant HTML, properly formatted, and be clear on what you are looking to get from the page. Be specific, give examples of desired output. – JeffC Apr 17 '21 at 15:06
  • @JeffC I am capturing screenshot and cropping the text, the button and the form on this website, only problem I am facing is I am getting text screenshot, but getting black screenshot for the form and button that's why I need to get that form and button and get a screenshot of it. The form can be accessed by tag_name but it is still not taking screenshot of it – Khasif Apr 17 '21 at 21:48
  • You are talking about screenshots but none of that is mentioned in your question. I'm even more confused now. You need to rewrite your question and make it clear 1) what you are trying to do (describe it with words, not code), 2) post your current code attempt and explain what it's output is, 3) explain what the expected output is, and then 4) post the relevant HTML (as text, properly formatted). Once you do those things, we can start to try to help you. – JeffC Apr 17 '21 at 22:09

2 Answers2

2

You want to print the Name, People, date, Message values of the form?

content = driver.find_element_by_tag_name('form')
for input in content.find_elements_by_xpath('./p/input'):
    print(input.get_attribute('name'))

The following would look for any html element with an attribute of name inside the form and print it.

content.find_elements_by_xpath('.//*[@name]')
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
  • Yes. but can you help me out with something else also, check out this post too. https://stackoverflow.com/questions/67143396/getting-black-screenshot-for-button-and-form – Khasif Apr 18 '21 at 01:56
0

You can use find_element_by_tag_name:

content = driver.find_element_by_tag_name('form')

This might have problems when there are multiple forms to choose from. Then you have to use find_elements_by_tag_name and potentially choose manually later.

MegaIng
  • 7,361
  • 1
  • 22
  • 35
  • But by printing the content it is showing this " " I still can't find the name , number and all the other elements – Khasif Apr 17 '21 at 13:32
  • @Khasif Yeah, you now want to get the children of the form which are the inputs. So do content.find_elements_by_tag_name('input') – MegaIng Apr 17 '21 at 13:53