1

I need to auto click on any of the "Add" buttons in a web page like as the following address:

"https://groceries.asda.com/search/yoghurt"

But, none of the "Add" buttons in the page has name or id. So I can not use driver.find_element_by_id() command from Selenium package.

Can any one help me?

Guy
  • 46,488
  • 10
  • 44
  • 88
user12235025
  • 55
  • 1
  • 6
  • Please read [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). Then read [ask] and take a few minutes to add some details to your question, especially an [mcve]. – JeffC Dec 12 '19 at 20:46

3 Answers3

2

To click on any particular Add button for a particular product you can write a method as follows:

def click_me(string):
    driver.find_element_by_xpath("//h3/a[@class='co-product__anchor' and contains(@title, '%s')]//following::button[1]" % (string)).click()

Now you can click on any of the Add button passing their title as follows:

click_me("Munch") # Munch Bunch Double Up Strawberry & Vanilla Yogurts
# or
click_me("ASDA") # ASDA Greek Style Fat Free Yogurt
# or
click_me("Petits") # Petits Filous Apricot, Strawberry & Raspberry Yogurt
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi The method works for me. Thanks. But, new problem occurs with this page. "https://saipa.iranecar.com/product-group/156/circulations/ticket/no-ticket" Can you help me to enter button on this page? – user12235025 Dec 13 '19 at 06:47
  • @user12235025 Trying to access https://saipa.iranecar.com/product-group/156/circulations/ticket/no-ticket gives me **`Access denied`** error. However it seems to be a new requirement. Can you raise anew question as per your new requirement please? Stackoverflow contributors will be happy to help you out. – undetected Selenium Dec 13 '19 at 07:01
  • I don't find hollow tick mark for voting you. Please show me what can i vote you? Yes. I forgot that this page prohibited from other countries. But the problem is as follows: When I load the page (just load), immediately after loading, it redirects to its parent page. I think that for the security options of the page, it recognizes the remote control and prevents other actions. My code: from selenium import webdriver import time import requests driver = webdriver.Firefox() url = 'https://saipa.iranecar.com/product-group/156/circulations/ticket/no-ticket' driver.get(url) – user12235025 Dec 13 '19 at 07:15
  • Have you any idea about leaving page that sent in last comment? – user12235025 Dec 13 '19 at 07:40
  • @user12235025 Not that super clear to me when you say _...leaving page that sent in last comment..._, do you have a question raised for that? – undetected Selenium Dec 13 '19 at 07:42
  • When I load the page (just load) by driver.get(url), immediately after loading the required url, it redirects to its parent page. I think that for the security options of the page, it recognizes the remote control and directs to another page. – user12235025 Dec 13 '19 at 07:48
0

Use a similar method find_elements_by_css_selector:

elements = driver.find_elements_by_css_selector(.asda-button.asda-button--category-primary.asda-button--color-green.asda-button--size-small.co-quantity__add-btn)

as the buttons have identifying classes. Afterwards, you can click each of these buttons:

for e in elements:
    e.click()
Arn
  • 1,898
  • 12
  • 26
  • Thanks a lot for your attention. I ran this code. As you see in the web page, there are many buttons with the name of "Add". But after running this code, the length of elements variable is zero. In fact, although the above css are in the list of button tag, the code can not find any element with these css. Do you have any idea? – user12235025 Dec 12 '19 at 20:32
  • Hi I did not wait for load page. The method works for me. Thanks. But, new problem occurs with this page. "https://saipa.iranecar.com/product-group/156/circulations/ticket/no-ticket" Can you help me to enter button on this page? – user12235025 Dec 13 '19 at 06:46
-1

are you saying you want to click on an add with python? for do that, you can do this:

enter code here
import pyautogui
x= #x location
y= #y location
while True:
     pyautogui.click(x,y)
  • Thanks a lot for your attention. Some of buttons do not always exist on the page. So, I must check for the existence (appearance) of the button. For this reason, I want to find element by its attributes, not with its position. Do you have any idea? – user12235025 Dec 12 '19 at 20:34