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

时间:2017-01-18 09:44:18

标签: python selenium-webdriver

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()

1 个答案:

答案 0 :(得分:0)

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

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;
    }

如何调用此方法

//i know outerHTML is too big just for example sake i have give  that you can provide any unique property name and unique property value which differentiates one button from other,outerHTML is one such property
IWebElement myButton = GetButtontoClick(prObj, "outerHtml", "outerHTMLValue")

希望这有效。