如何选择硒元素[不断变化的元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何选择硒元素[不断变化的元素相关的知识,希望对你有一定的参考价值。

我正在尝试浏览页面上6个元素的列表。但是,它的脚本似乎并没有按顺序执行,有时它会得到一个过时的元素异常。我试图在this page的计划类型之间切换。我希望能够单击“自带”>“小标签”>“中标签”>“大标签”>等,然后选择要抓取的其他元素。

    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 tab in planTypeTab:
            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)
答案

您可以尝试以下方法:

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')
def scrape_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 in zip(soup(d.page_source, 'html.parser').find_all('div', {'class':'filter-wrapper'}), d.find_elements_by_class_name('filter-wrapper')):
   b.send_keys('
')
   time.sleep(2)
   result[a.h3.text] = scrape_dropdown(soup(d.page_source, 'html.parser'))

输出:

{'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 & pict

以上是关于如何选择硒元素[不断变化的元素的主要内容,如果未能解决你的问题,请参考以下文章

如何解决硒循环中的这个错误? InvalidSelectorException:消息:无效选择器:无法使用 xpath 定位元素

硒如何获取文本大小

如何单击硒悬停后可见的元素?

如何等到硒python中的元素可用

如何等到硒中存在元素?

如何使用硒在网页上查找具有特定文本的所有输入元素