简体   繁体   English

在 selenium 中找到没有 id 的提交按钮

[英]find submit button in selenium without id

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.我无法通过 ID 或名称找到它,需要提交表格。

I tried doing this: Alternatively, WebDriver has the convenience method “submit” on every element.我尝试这样做:或者,WebDriver 在每个元素上都有方便的方法“提交”。 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.如果您在表单中的元素上调用 this,WebDriver 将遍历 DOM,直到找到封闭的表单,然后调用提交。 If the element isn't in a form, then the NoSuchElementException will be raised: element.submit() http://selenium-python.readthedocs.org/navigating.html如果元素不在表单中,则会引发 NoSuchElementException: element.submit() http://selenium-python.readthedocs.org/navigating.html

But that cannot find the submit selector either.但这也找不到提交选择器。

ideas?想法?

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:如果 class + value 组合是唯一的,则可以使用:

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

You can also use xpath:您还可以使用 xpath:

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

If none of those work (ie they are not identifying button uniquely), look at the elements above the button (for example <form ) and provide the xpath in format:如果这些都不起作用(即它们不是唯一标识按钮),请查看按钮上方的元素(例如<form )并以格式提供 xpath:

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

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我推荐 xpath chrome 扩展程序,有了它,您将能够通过运行扩展程序并按住 Shift 键单击您想要此 chrome 扩展程序的元素来获取路径https://chrome.google.com/webstore/detail/xpath-helper/ hgimnogjllphhhkhlmebbmlgjoejdpjl 在此处输入图片说明

您可以尝试使用 XPath 表达式或 CSS 选择器(如 input[type="button"])查找元素,然后单击该元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM