How To Select An Element [that Keeps Changing] With Selenium
I am attempting to move through a list of 6 elements on a page. However, its my script doesnt seem to being doing it sequentially and sometimes it will get a stale element excepti
Solution 1:
You can try this:
from selenium import webdriver
import time
from bs4 import BeautifulSoup as soup
d = webdriver.Chrome('/path/to/chromedriver')
d.get('https://www.koodomobile.com/rate-plans?INTCMP=KMNew_NavMenu_Shop_Plans')
defscrape_dropdown(page):
return [[b.get_text(strip=True) for b in i.find_all('div', {'class':'field-content'})] for i in page.find_all('div', {'class':'panel-2col-stacked clearfix panel-display'})]
result = {}
for a, b inzip(soup(d.page_source, 'html.parser').find_all('div', {'class':'filter-wrapper'}), d.find_elements_by_class_name('filter-wrapper')):
b.send_keys('\n')
time.sleep(2)
result[a.h3.text] = scrape_dropdown(soup(d.page_source, 'html.parser'))
Output:
{'Bring your own device': [['$30per month', '1 GBof Shock-Free Data', '0minutes', 'Unlimitedincoming messaging', '$30per month', '1 GBofShock-Free™ data.1,2', '0minutes', 'Unlimitedincoming messaging (text & picture)3. $0.60 per outgoing SMS and MMS.', 'Not eligible for tablet devices', 'Additional data $10/100 MB; additional talk rates $0.60/min', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$35per month', '250 MBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$35per month', '250 MBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$45per month', '1 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$45per month', '1 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$50per month', '2 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$50per month', '2 GBofShock-Free™ data1', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$55per month', '4 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$55per month', '4 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$65per month', '7 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$65per month', '7 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$75per month', '10 GBof Shock-Free Data.', 'Unlimitedminutes', 'Unlimitedmessaging', '$75per month', '10 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart']], 'Tab Small': [['$35per month', '250 MBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$35per month', '250 MBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$45per month', '1 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$45per month', '1 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$50per month', '2 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$50per month', '2 GBofShock-Free™ data1', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$55per month', '4 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$55per month', '4 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$65per month', '7 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$65per month', '7 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$75per month', '10 GBof Shock-Free Data.', 'Unlimitedminutes', 'Unlimitedmessaging', '$75per month', '10 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart']], 'Tab Medium': [['$45per month', '1 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$45per month', '1 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$50per month', '2 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$50per month', '2 GBofShock-Free™ data1', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$55per month', '4 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$55per month', '4 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$65per month', '7 GBof Shock-Free Data', 'Unlimitedminutes', 'Unlimitedmessaging', '$65per month', '7 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart'], ['$75per month', '10 GBof Shock-Free Data.', 'Unlimitedminutes', 'Unlimitedmessaging', '$75per month', '10 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart']], 'Tab Large': [['$75per month', '10 GBof Shock-Free Data.', 'Unlimitedminutes', 'Unlimitedmessaging', '$75per month', '10 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart']], 'Tab Extra Large': [['$75per month', '10 GBof Shock-Free Data.', 'Unlimitedminutes', 'Unlimitedmessaging', '$75per month', '10 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart']], 'Tab Extra Extra Large': [['$75per month', '10 GBof Shock-Free Data.', 'Unlimitedminutes', 'Unlimitedmessaging', '$75per month', '10 GBofShock-Free™ data.1,2', 'UnlimitedCanada-wide anytime minutes', 'Unlimitedmessaging (text & picture)3', 'UnlimitedCanada-wide anytime minutesUnlimitedmessaging (text & picture)3Call Display, Voicemail, Call Waiting and Conference Calling', 'Call Display, Voicemail, Call Waiting and Conference Calling', 'Additional data $10/100 MB', '$35 connection fee (including SIM card) applies when purchased in store', '$0 connection fee (including SIM card) when you order online!', 'Add to cart']]}
Solution 2:
You should put the action of find element in for loop to avoid stale element exception.
def planDicts():
# maxTablink = maxTabLink() #finds a link with all the plans
driver.get('https://www.koodomobile.com/rate-plans?INTCMP=KMNew_NavMenu_Shop_Plans')
planTypeTab = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.filter-wrapper')))
for i in range(1, len(planTypeTab)+1):
tab = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.filter-wrapper:nth-child('+i+')')))
driver.execute_script("arguments[0].scrollIntoView();", tab)
actions = ActionChains(driver)
actions.move_to_element(tab)
actions.click()
actions.perform()
planName = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#block-system-main > div > div > div.view-content > div.rate-plan-block > div.tab-header-wrapper > h2')))
planName = planName[0].text
print(planName)
Post a Comment for "How To Select An Element [that Keeps Changing] With Selenium"