在Python中单击具有相同类名的多个按钮 [英] click multiple buttons with same class names in Python

查看:221
本文介绍了在Python中单击具有相同类名的多个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This a column in a table this column contains buttons, on pressing each buttons a pdf is downloaded

这些按钮具有相同的类名,我想单击所有按钮.

The buttons have the same class names and I want to click on all the buttons.

这是我所做的,但是没有用:

This is what I did, but it doesnt work:

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

推荐答案

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

Hi First get the reference of the parent container in which all these buttons reside and then you will have to get the button reference which you would like to click by passing unique parameters to the method which identities the button which you want to click

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

希望这行得通.

这篇关于在Python中单击具有相同类名的多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆