自动单击网页中的按钮

问题描述

我需要自动点击网页中的任何添加"按钮,如下地址:

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

但是,页面中的添加"按钮都没有名称或 ID.所以我不能使用 Selenium 包中的 driver.find_element_by_id() 命令.

谁能帮帮我?

解决方案

要单击特定产品的任何特定 Add 按钮,您可以编写如下方法:

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

现在您可以点击任何一个 Add 按钮来传递它们的标题,如下所示:

click_me("Munch") # Munch Bunch Double Up Strawberry &香草酸奶# 或者click_me("ASDA") # ASDA 希腊式脱脂酸奶# 或者click_me("Petits") # Petits Filous Apricot, Strawberry &覆盆子酸奶

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?

解决方案

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

相关文章