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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35531069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:33:33  来源:igfitidea点击:

find submit button in selenium without id

pythonselenium

提问by humanbeing

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. 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

我尝试这样做:或者,WebDriver 在每个元素上都有方便的方法“提交”。如果您在表单中的元素上调用 this,WebDriver 将遍历 DOM,直到找到封闭的表单,然后调用提交。如果元素不在表单中,则会引发 NoSuchElementException: element.submit() http://selenium-python.readthedocs.org/navigating.html

But that cannot find the submit selector either.

但这也找不到提交选择器。

ideas?

想法?

采纳答案by Kiril S.

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 (i.e. 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()

回答by NicoPaez

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.

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

回答by Nelson Bwogora

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/hgimnogjllphhhkhlmebbmlgjoejdpjlenter image description here

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