在Python中单击具有相同类名的多个按钮

2024-10-04 01:26:17 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

此列为表格中的一列此列包含按钮,按每个按钮时将下载pdf

按钮有相同的类名,我想点击所有的按钮。在

我就是这么做的,但没用:

addinfo = driver.find_element_by_class_name('btnAddInfo')
for x in addinfo:
    if addinfo[x].is_displayed():
        addinfo[x].click()

Tags: nameinforbyifpdfdriverelement
1条回答
网友
1楼 · 发布于 2024-10-04 01:26:17

首先获取所有这些按钮所在的父容器的引用,然后必须通过向标识要单击的按钮的方法传递唯一参数来获取要单击的按钮引用

public IWebElement GetButtontoClick(IWebElement prObj, string propName, string propVlu)
    {
        IWebElement btnToClick = null;
        try
        {
            //get the list of all buttons in the prObj
            IList<IWebElement> btnlList = prObj.FindElements(By.ClassName("xyz"));

            //iterate through each button element and find the button you want to click
            for (int i = 0; i < btnlList.Count; i++)
            {
                IWebElement btn = btnlList[i];

                var btnPropValue = ((IJavaScriptExecutor)DriverContext.Driver).ExecuteScript("return arguments[0]."+ propName+"; ", btn);
                if (propVlu == btnPropValue.ToString())
                {
                    Console.WriteLine("You have found the button you want to click");
                    btnToClick = btn;
                    break;
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        return btnToClick;
    }

如何调用此方法

^{pr2}$

希望这能奏效。在

相关问题 更多 >