I've got a form with a bunch of items. Beside each item is a "Remove" button. When one of buttons is pressed, I need to know which one so that I know which item to remove.
The way I have it now, each button is named remove-# where # is the index of the item. In order to grab the # though I have to loop over every POST value, check if the key starts with remove- and if it does, parse out the integer.
Is there any method that would be faster than O(n) where n is the number of post vars?
Not that n is so large that this would become an expensive operation, but mostly because I'm curious.
Also, before I get any answers that suggest I use JavaScript and put an onclick event on each button to move the index into a separate hidden input.... I'm trying to do this without JavaScript.
An option would be to put each remove button in a separate form, with a hidden value with a constant name and the value indicating which item to remove.
If the POST values are stored in a dict you could turn this around and instead of looking for something that looks like a remove-# you generate all remove-# and try to use them to index the dict. That would limit the search to O(m) where m is the number of elements that can be removed.
This really is the domain of Javascript. However adding onclick isn't a great way to do this. Use HTML5 data attributes and unobtrusive listeners.. For example, in JQuery/HTML5
<input type='button' class='remove-button' name='remove' data-remove-attr='5'>
$('.remove-button').live("click", function() {
alert($(this).attr("data-remove-attr")); // Alerts 5.
});
Despite the data- being an HTML5 thing, it is backwards compatible with old IE, etc as they always allowed arbitrary dom attributes.
Edit: Note this is O(1) if as your action you did $('#remove-'+id).hide();
Related
I have some entries that are to be made in a web portal.
The entries that are to be made are in excel file. I have imported those in python and converted them to lists so that I can access them to pick up individual entry.
Will try to explain code approach here
find first element and use send keys to first element of the list
same for next two fields and then save the entry.
(ignore syntax in below)
driver.find_element_by_name("01st elementname").send_keys(list1[0])
driver.find_element_by_name("02nd elementname").send_keys(list2[0])
driver.find_element_by_name("03rd elementname").send_keys(list3[0])
Till this portion is done.
Next I have to move to second and make entry with next index
driver.find_element_by_name("01st elementname").send_keys(list1[1])
driver.find_element_by_name("02nd elementname").send_keys(list2[1])
driver.find_element_by_name("03rd elementname").send_keys(list3[1])
move to next.
How can I do this ? Not being able to figure out for loop for this.
I hope I explained this well. Could be very simple but I am not from programming background so need some help.
You mean how to loop over the list like this?
for i in range(len(list1)):
driver.find_element_by_name("01st elementname").send_keys(list1[i])
driver.find_element_by_name("02nd elementname").send_keys(list2[i])
driver.find_element_by_name("03rd elementname").send_keys(list3[i])
//submit click() if required
See also https://www.w3schools.com/python/python_for_loops.asp
Ok so I had a list of web items created by seleniums Webdriver.find_elements_by_path method, and I had trouble utilizing the data.
Ultimately, the code I needed to get what I wanted was this:
menu_items=driver.find_elements_by_xpath('//div[#role="menuitem"]')[-2]
I was only ever able to get any meaningful data here by using a negative index. If I used any positive indices, the menu_items would return nothing.
However, when I had left menu_items as follows:
menu_items=driver.find_elements_by_xpath('//div[#role="menuitem"]')
I could iterate through the list and gain access to the webelements properly, meaning if I had"for i in menu_items" I could call something like i.text and have the desired result. But again, I could not do menu_items[2]. I am new to selenium so if someone could explain what is going on here, that would be very helpful
This line of code...
menu_items=driver.find_elements_by_xpath('//div[#role="menuitem"]')[-2]
...indicates you are considering the second element counting from the right instead of the left as list[-1] refers to the last element within the list and list[-2] refers to the second last element in the list.
A bit more about your usecase would have helped us to construct a canonical answer. The number of visible/interactable elements at any given point of time and/or the sequence in which the elements gets visible/interactable may vary based on the type of elements present in the DOM Tree. Incase the HTML DOM consists of JavaScript, Angular, ReactJS, enabled elements even the position of the elements may differ as well.
I have a dictionary which contains (roughly) 6 elements, each of an element which looks like the following:
What I want to do is find a particular domain (that I pass through a method) and if it exists, it stores the keyword and its position within an object. I have tried the following
def parseGoogleResponse(response, website):
i = 0
for item in response['items']:
if(item['formattedUrl'] == website):
print i
break;
i++
This approach seems to be a bit tedious and also i also remains the same at i = 10 and I'm pretty sure that this is a more efficient way. I also have to keep in consideration that if the website is not found the first time, it then queries the API for a maximum up to 5 pages, each page contains 6 search results so I somehow have to calculate the position if it is on a different page.
Any ideas
Dictionaries in Python are not ordered. There is no way to find something's position in a dictionary, unlike list type objects.
You can rather easily check for the existence of a value in the dictionary with something like:
if website in response['items'].values():
# If you enter this section, you know it's in the dictionary
else:
# If you end up here, it isn't in the dictionary
I was wondering if it is possible to re-select each and every item in the rsList?
I am citing a simple example below but I am looking at hundreds of items in the scene and hence below are the simplest form of coding I am able to come up with base on my limited knowledge of Python
rsList = cmds.ls(type='resShdrSrf')
# Output: [u'pCube1_GenShdr', u'pPlane1_GenShdr', u'pSphere1_GenShdr']
I tried using the following cmds.select but it is taking my last selection (in memory) - pSphere1_GenShdr into account while forgetting the other 2 even though all three items are seen selected in the UI.
Tried using a list and append, but it also does not seems to be working and the selection remains the same...
list = []
for item in rsList:
list.append(item)
cmds.select(items)
#cmds.select(list)
As such, will it be possible for me to perform a cmds.select on each of the item individually?
if your trying to just select each item:
import pymel.core as pm
for i in pm.ls(sl=True):
i.select()
but this should have no effect in your rendering
I think for mine, it is a special case in which I would need to add in mm.eval("autoUpdateAttrEd;") for the first creation of my shader before I can duplicate.
Apparently I need this command in order to get it to work
I'm using python for my shopping cart class which has a list of items. When a customer wants to edit an item, I need to pass the JavaScript front-end some way to refer to the item so that it can call AJAX methods to manipulate it.
Basically, I need a simple way to point to a particular item that isn't its index, and isn't a reference to the object itself.
I can't use an index, because another item in the list might be added or removed while the identifier is "held" by the front end. If I were to pass the index forward, if an item got deleted from the list then that index wouldn't point to the right object.
One solution seems to be to use UUIDs, but that seems particularly heavyweight for a very small list. What's the simplest/best way to do this?
Instead of using a list, why not use a dictionary and use small integers as the keys? Adding and removing items from the dictionary will not change the indices into the dictionary. You will want to keep one value in the dictionary that lets you know what the next assigned index will be.
A UUID seems perfect for this. Why don't you want to do that?
Do the items have any sort of product_id? Can the shopping cart have more than one of the same product_id, or does it store a quantity? What I'm getting at is: If product_id's in the cart are unique, you can just use that.