Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

selenium webdriver - click multiple buttons with same class names in Python

enter image description here

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

How to call this method

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

Hope this works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...