click multiple buttons with same class names in Python

2024/11/16 21:52:23

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

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 prObjIList<IWebElement> btnlList = prObj.FindElements(By.ClassName("xyz"));//iterate through each button element and find the button you want to clickfor (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.

https://en.xdnf.cn/q/120283.html

Related Q&A

Python equivalent to subset function in r [duplicate]

This question already has answers here:subsetting a Python DataFrame(6 answers)Closed 4 years ago.I dont know python at all but the project Im currently working on must be done using it, I have this r …

Force subprocess to use Python 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Python: Im making a simple calculator for class. Whats wrong with this code? [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Closed 7 months ago.My teacher requests me to make a calculator that can calculate a 15% tip on a submitted price. I…

FileNotFoundError Python Script

I am trying to run a python script, .py in the windows command prompt. I drag the script from my files into the command prompt window. I run it. Then, the script presents a prompt for me to enter the f…

Filtered product of lists without repetitions

I need an efficient solution to find all combinations without repetitions of a given lists. It has to work like this: l1 = [1, 2, 3] l2 = [3, 4, 5] combinations(l1, l2) = [(2, 4), (3, 4), (1, 5), (1, 4…

int object is not callable only appears randomly

Sometimes this code works just fine and runs through, but other times it throws the int object not callable error. I am not real sure as to why it is doing so.for ship in ships:vert_or_horz = randint(0…

scraping css values using scrapy framework

Is there a way to scrap css values while scraping using python scrapy framework or by using php scraping. any help will be appreaciated

Access dict via dict.key

I created a dict source = {livemode: False}. I thought its possible to access the livemode value via source.livemode. But it doesnt work. Is there a way to access it that way?As a not source[livemode]…

Function not returning anything

My viewdef login(request):c = {}c.update(csrf(request))return render_to_response(request, login.html, c)def auth_view(request):username = request.POST.get (username, )password = request.POST.get (passw…

My entry box always returns PY_VAR1 value!!though Im using the .get function

please take a look at my code, its really simple I need to take the value from the entry box and use it in my program and when pressing the add button I print it ,it keeps giving me this value PY_VAR1…