26

I have a button

<input type="submit" class="button button_main" style="margin-left: 1.5rem;" value="something">

I cannot find it by id or name and need to submit a form.

I tried doing this: Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be raised: element.submit() http://selenium-python.readthedocs.org/navigating.html

But that cannot find the submit selector either.

ideas?

1
  • 1
    is the form inside an iframe? Commented Feb 21, 2016 at 1:15

4 Answers 4

62

There are many options here, to name a few:

If class alone is unique, you can use

driver.find_element_by_css_selector(".button_main").click()

If class + value combo is unique, you can use:

driver.find_element_by_css_selector(".button_main[value='something']").click()

You can also use xpath:

driver.find_element_by_xpath("//input[@type='submit' and @value='something']").click()

If none of those work (i.e. they are not identifying button uniquely), look at the elements above the button (for example <form) and provide the xpath in format:

driver.find_element_by_xpath("//unique_parent//input[@type="submit" and @value='something']").click()
Sign up to request clarification or add additional context in comments.

1 Comment

Now deprecated for anyone trying this
3

i recommend xpath chrome extension, with it you will be able to get the path by running the extension and shift-clicking on the element you want this chrome extension https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl enter image description here

2 Comments

You don’t need an extension for this, just open developer tools and select an element, then right-click > copy > xpath. To search by xpath just use ctrl + f or cmd + f
I wouldn't recommend using this method: xpath recommendations provided by developer tools (and some other tools) are not good xpaths to use with Selenium. They generate absolute xpath, while it's much better to use relative xpath (absolute xpath is far more breakable than relative one; it's also much harder to read and semantically may be unclear). So you can use this generated xpath as a basis, but not as an actual value.
1

You could try to find the element with an XPath expression or a CSS selector like input[type="button"], and then just click the element.

Comments

0

At time of writing this is working for me...

let submit = await driver.findElement(By.css("[type='submit']"));
await submit.click();

Luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.