Mr 2
Mr 2

Reputation: 33

Selenium find element with form action

I am currently trying out Selenium to develop a program to automate testing of the login form of websites.

I am trying to use Selenium to find a form on the websites that I am testing on and I've noticed that different websites has different form name, form id or even websites that doesn't have both. But from my observations, I've noticed that form action is always there and I've used the codes below to retrieve the name of the form action

request = requests.get("whicheverwebsite")
parseHTML = BeautifulSoup(request.text, 'html.parser')
htmlForm = parseHTML.form
formName = htmlForm['action'] 

I am trying to retrieve the form and then using form.submit() to submit.

I know of the functions find_element_by_name and find_element_by_name, but as I am trying to find the element by action and i am not sure how this can be done.

Upvotes: 1

Views: 7227

Answers (3)

Alex Pahom
Alex Pahom

Reputation: 11

Keep in mind that login page can have multiple form tags even if you see only one. Here is the Example when login page has only one visible form though there are 3 ones in DOM.

So the most reliable way is to dig into the form (if there are multiple ones) and check two things:

  • If there's [type=password] element (we definitely need a password to log in)
  • If there's the 2nd input there (though it can be considered as optional)

Ruby example:

forms = page.all(:xpath, '//form')  # retrieve all the forms and iterate
forms.each do |form|
   # if there's a password field             if there's two input fields in general
  if form.has_css?('input[type=password']) && form.all(:xpath, '//input').count == 2)
    return form
  end
end

Upvotes: 0

Mr 2
Mr 2

Reputation: 33

I've found the answer to this.

By using xpath and using form and action, I am able to achieve this.

form = driver.find_element_by_xpath("//form[@action='" + formName + "']")

Upvotes: 2

sjuice10
sjuice10

Reputation: 39

I would recommend including the url of one or two of the sites you are trying to scrape and your full code. Based on the information above, it appears that you are using BeautifulSoup rather then Selenium.

I would use the following:

from selenium import webdriver


url = 'https://whicheverwebsiteyouareusing.com'
driver = webdriver.Chrome()

driver.get(url)

From there you have many options to select the form, but again, without the actual site we can't identify which would be most relevant. I would recommend reading https://selenium-python.readthedocs.io/locating-elements.html to find out which would be most applicable to your situation.

Hope this helps.

Upvotes: 0

Related Questions