0

I want to find all buttons on the page which contain the word "card" or the word "bag".

For example it could be "add to card" or "add to bag" or "card" etc.

from selenium import webdriver 
from selenium.webdriver.support.color import Color
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome(executable_path = ChromeDriverManager().install())
driver.get('https://www.inshoes.gr/andrikes-zones-andrikes-zones-dermatines-diplis-opseos-33000481-kamel-mavro')
buttons=driver.find_element_by_xpath('//button[normalize-space()="Click me!"]')

for button in buttons:
    print (button)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • For example it could be "add to card" or "add to bag" or "card" etc. - Which buttons you are looking please specify them. – cruisepandey Dec 21 '21 at 13:18

2 Answers2

0

I use the following for this exact use case (for accepting cookies):

buttons = driver.find_elements_by_xpath("//button[contains(text(), 'Accept cookies')]") # Try Click Cookies

You can interchange the text as you like.

Sam
  • 773
  • 4
  • 13
  • Thank you! And how I can get the backfround color of this? – user15924442 Dec 21 '21 at 12:56
  • Sorry I'm not quite sure what you mean, the background colour of the button? Could you show me an example of one of the buttons (in terms of the actual html). – Sam Dec 21 '21 at 13:00
  • fI want to have a list of websites and for each one I want to find the "Add to card button" and find the color of it – user15924442 Dec 21 '21 at 13:03
  • Understood, I need to see an example of one of the html tags that you're trying to scrape from so I can understand the structure of it. – Sam Dec 21 '21 at 13:20
  • overview .add-to-cart-button:hover, .variant-overview .add-to-cart-button:hover { background-color: #b41733; – user15924442 Dec 21 '21 at 13:24
  • Could you please link the website and the desired button so I can take a look? Sorry for the confusion. – Sam Dec 21 '21 at 13:31
  • https://www.inshoes.gr/andrikes-zones-andrikes-zones-dermatines-diplis-opseos-33000481-kamel-mavro – user15924442 Dec 21 '21 at 13:32
0

To find all buttons on the page which contain the word card or the word bag you can clubup the conditions using within a lambda expression and you can use the following Locator Strategy:

elements = WebDriverWait(driver,20).until(lambda driver: driver.find_element(By.XPATH,"//button[contains(., 'card')]") or driver.find_element(By.XPATH,"//button[contains(., 'bag')]"))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352