I have: two lists of unused and used elements. I have to transfer exact elements (element can be transferred on double click or click + button press than it disappears from 'unused elements' and appears in 'used elements') from first one to second. Discovered that first list is reacquired each time when I transfer element.
for x in range(0, len(offers)):
try:
if "21796" in offers[x].get_attribute('text'):
ActionChains(app.driver).double_click(offers[x]).perform()
offers = app.driver.find_elements_by_css_selector('select[name="unused_offers[]"]>option')
except IndexError:
break
So I run through the cycle in range of length of initial offers list and there are two problems:
1) If I won't reacquire 'offers' list I'll get StaleElementException
2) If I do I'll get out of initial offers list range because each iteration 'offers' list become shorter.
I decided to go through 2) way and just handle IndexError exception.
The question: Is there a better way to iterate through the list which becomes shorter then iteration range?
I did try also
ActionChains(app.driver).key_down(Keys.CONTROL, offers[x]).click(offers[x]).key_up(Keys.CONTROL, offers[x]).perform()
In cycle for escape list reacquire but there was the issue - cycle was just clicking elements one by one (looks like CTRL wasn't really held.
I would make an "endless" loop, find an offer on each iteration and exit the loop once an offer cannot be found (NoSuchElementException exception is raised). Implementation:
from selenium.common.exceptions import NoSuchElementException
while True:
try:
offer = app.driver.find_element_by_xpath('//select[@name="unused_offers[]"]/option[contains(@text, "21796")]')
except NoSuchElementException:
break
ActionChains(app.driver).double_click(offer).perform()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With