0

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?

I am learning Selenium maybe someone could answer me that question it would be very nice. Thank you!

1

4 Answers 4

1

You can click using class name, like this:

  1. 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 docs can be helpful: https://selenium-python.readthedocs.io/locating-elements.html

Sign up to request clarification or add additional context in comments.

Comments

0

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):

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:

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.

Comments

0

Selenium can locate elements based not only on their id values.
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:

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.

Comments

0

You can construct a robust xpath like this :

//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()

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.