问题
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?
回答1:
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
回答2:
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()
回答3:
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)
来源:https://stackoverflow.com/questions/59311956/auto-click-a-button-in-a-web-page