简体   繁体   English

Selenium Python 点击没有 ID 的按钮

[英]Selenium Python click a Button without ID

I have a Button:我有一个按钮:

<button type="button" class="Buttonstyled__StyledButton-sc-140xkaw-1 bbPePk css-1n4k82t">Zum Warenkorb</button>

I want to click this Button but I don't have the id are there other ways?我想点击这个按钮,但我没有 ID 还有其他方法吗?

I am learning Selenium maybe someone could answer me that question it would be very nice.我正在学习 Selenium 也许有人能回答我这个问题会很好。 Thank you!谢谢你!

You can click using class name, like this:您可以使用 class 名称单击,如下所示:

  1. Select Select

     button = driver.find_element_by_class_name('Buttonstyled__StyledButton-sc-140xkaw-1 bbPePk css-1n4k82t')
  2. Execute:执行:

button.click()

In this example, 'driver' is an instance of selenium using Firefox Web driver在此示例中,“驱动程序”是 selenium 使用 Firefox Web 驱动程序的实例

Selenium docs can be helpful: https://selenium-python.readthedocs.io/locating-elements.html Selenium 文档可能会有所帮助: https://selenium-python.readthedocs.io/locating-elements.html

You can actually look for the button class to find it (but it returns a list of all of the elements with that class since classes aren't unique):您实际上可以查找按钮 class 来找到它(但它会返回包含该 class 的所有元素的列表,因为类不是唯一的):

driver.find_elements_by_css_selector('.theClass')

Details can be found here https://www.geeksforgeeks.org/find_elements_by_css_selector-driver-method-selenium-python/ , but assuming it's the first one in the page you can use:可以在此处找到详细信息https://www.geeksforgeeks.org/find_elements_by_css_selector-driver-method-selenium-python/ ,但假设它是您可以使用的页面中的第一个:

driver.find_elements_by_css_selector('.theClass')[0]

In your case it would be:在您的情况下,它将是:

driver.find_elements_by_css_selector(".Buttonstyled__StyledButton-sc-140xkaw-1")[0]

Assuming it is the first one in the page.假设它是页面中的第一个。 If it's not the first one with that class then you will have to choose which one in the list returned by the method is the correct one.如果它不是第一个带有 class 的,那么您将必须选择该方法返回的列表中的哪个是正确的。

Selenium can locate elements based not only on their id values. Selenium 不仅可以根据元素的 id 值来定位元素。
Locator can be based on any unique tag name, any element attribute and any relation to some other elements.定位符可以基于任何唯一的标签名称、任何元素属性以及与某些其他元素的任何关系。
In this particular case it looks that your element contains unique text.在这种特殊情况下,您的元素看起来包含独特的文本。
If so, you can locate it by XPath based on that text as following:如果是这样,您可以根据该文本通过 XPath 找到它,如下所示:

driver.find_elements_by_xpath('//button[text()="Zum Warenkorb"]').click()

You should also add some wait / delay before accessing this element to let the element be fully loaded before you going to click on it.您还应该在访问此元素之前添加一些等待/延迟,以便在您单击它之前让该元素完全加载。

You can construct a robust xpath like this:您可以像这样构造一个健壮的xpath

//button[contains(@class,'Buttonstyled__StyledButton') and text()='Zum Warenkorb']

and then use it like this:然后像这样使用它:

driver.find_element_by_xpath("//button[contains(@class,'Buttonstyled__StyledButton') and text()='Zum Warenkorb']").click()

or if it requires explicit wait then:或者如果它需要显式等待则:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 50)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class,'Buttonstyled__StyledButton') and text()='Zum Warenkorb']"))).click()

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

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