自动单击网页中的按钮

2023-07-04Python开发问题
30

本文介绍了自动单击网页中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

"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

这篇关于自动单击网页中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11