【问题标题】:Selenium | How to iterate | TypeError: 'WebElement' object is not iterable硒 |如何迭代 | TypeError:“WebElement”对象不可迭代
【发布时间】:2021-10-25 17:34:50
【问题描述】:

请在下面找到附加的屏幕截图,我想获取行,即所有推文行。 我正在努力如何迭代和获取所有行。

下面的代码我试过了。

driver.get("https://www.trackmyhashtag.com/")

form = driver.find_element_by_xpath("//*[@class='container']")
usrinput = driver.find_element_by_xpath('/html/body/header/div[2]/div/div[2]/form/div/div[1]/input')
usrinput.clear()
usrinput.send_keys("india")

loginbt = driver.find_element_by_xpath('/html/body/header/div[2]/div/div[2]/form/div/div[2]/button')
loginbt.click()
time.sleep(5)

ad = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div[3]/div/div/div/button')
ad.click()
time.sleep(5)

element = driver.find_element_by_xpath('/html/body/aside/nav/div/ul/li[2]/a')
actions = ActionChains(driver)
actions.move_to_element(element).perform()
actions.click().perform()
time.sleep(5)

ad=wait.until(driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div[3]/div/div/div/button'))

row = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[5]/div/div/div/div[2]/div')

for i in row:
    i.get_attribute('td')
    print(i)

print("inside it")

【问题讨论】:

  • 试试这个 xPath //table[@id='tweets_table']//tr rowrow = driver.find_elements_by_xpath
  • 试过了!没有运气。同样的错误 TypeError: 'WebElement' object is not iterable
  • 试试更新的..你需要使用查找元素
  • @pkk 你试过我的答案了吗?这是一个错字,已经改正了。
  • 是的!但它给出了一些例外。请在您的答案中找到评论。

标签: python selenium selenium-webdriver selenium-chromedriver iterable


【解决方案1】:

这个例外:

TypeError: 'WebElement' 对象不可迭代

因为你应该使用find_elements,它返回一个列表,并且可以是可迭代的,而不是find_element,它返回一个单一的网络元素。

代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

options = webdriver.ChromeOptions()
options.add_argument("--disable-infobars")
options.add_argument("--disable-notifications")
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 2})
options.add_argument('--window-size=1920,1080')
options.add_experimental_option("prefs", {"profile.default_content_settings.cookies": 2})
driver = webdriver.Chrome(executable_path = driver_path, options = options)
#driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(50)
driver.get("https://www.trackmyhashtag.com/")
wait = WebDriverWait(driver, 10)

actions = ActionChains(driver)
wait.until(EC.visibility_of_element_located((By.ID, "search_keyword"))).send_keys("India", Keys.RETURN)

def close_up():
    time.sleep(1)
    actions.move_to_element(wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@data-dismiss='modal']"))))
    button = driver.find_element_by_xpath("//button[@data-dismiss='modal']")
    driver.execute_script("arguments[0].click();", button)
    time.sleep(1)

def check_model_winodows():
    try:
        if len(driver.find_elements(By.XPATH, "(//button[@data-dismiss='modal'])[1]")) >0:
            print("Pop up is visible")
            close_up()
        else:
            print("Pop up is not visible")
    except:
        print("Something went wrong")
        pass

check_model_winodows()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='preview-tweets']"))).click()
check_model_winodows()
total_number_of_tweet_row = len(driver.find_elements(By.XPATH, "//tbody/tr"))
print(total_number_of_tweet_row)

j = 0
    for i in range(total_number_of_tweet_row):
        check_model_winodows()
        elems = driver.find_elements(By.XPATH, "//tbody/tr")
        time.sleep(1)
        final_ele = elems[j].find_element_by_xpath(".//td[2]")
        print("code worked till here")
        print(final_ele.text)
        j = j + 1
        check_model_winodows()

【讨论】:

    【解决方案2】:
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait, Select
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome("/usr/local/bin/chromedriver")
    driver.get("https://www.trackmyhashtag.com/")
    
    form = driver.find_element_by_xpath("//*[@class='container']")
    usrinput = driver.find_element_by_xpath('/html/body/header/div[2]/div/div[2]/form/div/div[1]/input')
    usrinput.clear()
    usrinput.send_keys("india")
    
    loginbt = driver.find_element_by_xpath('/html/body/header/div[2]/div/div[2]/form/div/div[2]/button')
    loginbt.click()
    
    element = driver.find_element_by_xpath('/html/body/aside/nav/div/ul/li[2]/a')
    actions = webdriver.ActionChains(driver)
    actions.move_to_element(element).perform()
    actions.click().perform()
    
    rows = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'tbody > tr')))
    
    for row in rows:
        tds = row.find_elements(By.TAG_NAME, 'td')
        if len(tds) > 0:
            for td in tds:
                print(td.text.strip())
                    
    driver.quit()
    

    输出:

    Waheed
    @Waheedullahsha
    RT @News18Urdu: صدر جمعیۃ علما ہند مولانا ارشدمدنی نے آج مظفر نگر فساد میں بے گھر ہوئ...
    25 Aug 2021
    06:29:52 PM
    2
    376
    
    Abhijeet D. Sangle
    @ADSangl3
    RT @sarbanandsonwal: Tomorrow, we will be signing an agreement with Hooghly Cochin Shipyard Limited for Ship Repair Facility (Slipway) at P?...
    25 Aug 2021
    06:29:52 PM
    31
    304
    
    ck@chandan
    @Ck__ChaNDaN
    78 at Headingley is #TeamIndia's 9th lowest Test total of all-time, eight months after recording their lowest ever score of 36 in Australia....
    25 Aug 2021
    06:29:52 PM
    0
    35
    
    Vinod
    @VinodpalP
    RT @vivekagnihotri: 100% lobbying for vegan brands, almond milk etc. They just want @Amul_Coop to shut down. India is the largest milk prod?...
    25 Aug 2021
    06:29:51 PM
    297
    378
    
    Akash Dwivedi
    @akashdwivedi97
    RT @narendramodi: Chaired the 37th PRAGATI session during which projects with over Rs. 1,26,000 crore across 14 states were reviewed. We al?...
    25 Aug 2021
    06:29:51 PM
    2429
    2501
    
    raj skf
    @SkfRaj
    RT @HEAD__OF__TABLE: Swimming could have been an alternative career for Salman. He was a swimming champion at school and even tipped to rep?...
    25 Aug 2021
    06:29:51 PM
    2
    3
    
    .....Arron..... ??
    @arron_sharma
    RT @noconversion: India का कुछ नहीं हो सकता
    25 Aug 2021
    06:29:51 PM
    908
    956
    
    JS Bits
    @js_bits
    RT @richardkimphd: 5 Things India Should Learn from The US's #AI and ML Policy? 2021-08-24T14:30:35Z #NaturalLanguageProcessing #Matplotli?...
    25 Aug 2021
    06:29:50 PM
    3
    3132
    
    Naveen Dass
    @NaveenD05684789
    RT @Lalitamanikpur1: "TOBACCO IS THE BIGGEST HINDRANCE IN PATH OF BHAKTI" Visit our YouTube Channel: Sant Rampal Ji Maharaj #GodMorningWedn?...
    25 Aug 2021
    06:29:50 PM
    24
    497
    
    Nagendra Pathak ( ADV )
    @nagendrapathak
    RT @somnath456: @Somnath08976511 @nm0565593 @Krishna9936170 @Ashokaaa5 @opsethi @harbanssaini19 @yashwant9617612 @VijaySi51 @skn_337 @MrsMe?...
    25 Aug 2021
    06:29:50 PM
    2
    1513
    
    Sanjeeb Patra
    @sanjeeb603148
    RT @narendramodi: Best of luck India! I am sure our #Paralympics contingent will give their best and inspire others.
    25 Aug 2021
    06:29:50 PM
    10768
    10779
    
    মন্নু ঝা
    @man123kar
    Mother tongue is one of the unique identity of the people. Union of India's beautiful linguistic diversity. No one has the right to impose t...
    25 Aug 2021
    06:29:49 PM
    0
    15
    
    Khavatari
    @Hrolf_Kraki
    RT @alomgc14: Alonso en Spa 2013 ?? [1/3] Salidón en un circuito difícil donde es difícil hacerlas. Se funde al Force India, a los...
    25 Aug 2021
    06:29:49 PM
    12
    765
    
    Wisal Muhammad Khan
    @IbneDost
    RT @CynthiaDRitchie: India's own Congress stating Modi ji could not care less about the people's progress or prosperity. Their hashtag: #I?...
    25 Aug 2021
    06:29:49 PM
    7
    401
    
    Premnath Bhardwaj
    @PremnathBhardw8
    A powerful jihadi channel of India.
    25 Aug 2021
    06:29:49 PM
    0
    117
    
    Zoe
    @mysticlionheart
    RT @GabbbarSingh: Vikram Rathore is India's batting choke.
    25 Aug 2021
    06:29:49 PM
    2
    47
    
    .
    @Arv_Ga
    RT @Kishlaysharma: No matter what India's fate tomorrow . I will always be able to say that I was NOT a part of this destruction & I wa...
    25 Aug 2021
    06:29:49 PM
    9
    630
    
    P-A
    @BigDreamBigShot
    RT @FreedomIsrael_: One of these countries prescribes #VACCINES, the other prescribes #IVERMECTIN Retweet if you know which is which. Clu?...
    25 Aug 2021
    06:29:48 PM
    298
    332
    
    Larry Wu
    @lwu0303
    @_IndianPatriot @_ChachaChaudhry @PomPomSlappy @Anna30253409 @BasavaprasadNa8 @globaltimesnews Police brutality in india
    25 Aug 2021
    06:29:48 PM
    0
    74
    
    India Covid Vaccine Availability
    @india_vac
    MUMBAI: 79933 slots found for age 18+! Aug 25, 2021: 440 slots found in pincode 400026 at 3 centers Aug 25, 2021: 38 slots found in pincode ...
    25 Aug 2021
    06:29:48 PM
    0
    453
    

    【讨论】:

      【解决方案3】:

      要获取所有推文行,您可以使用它

      rows = driver.find_elements_by_xpath("//tr[@role='row' and(@class)]")
      

      现在要获取例如推文名称,您可以这样做:

      actions = ActionChains(driver)
      
      rows = driver.find_elements_by_xpath("//tr[@role='row' and(@class)]")
      for i in range(1,len(rows)+1):
          row = driver.find_element_by_xpath("(//tr[@role='row' and(@class)])[" + str(i) + "]")
          actions.move_to_element(row).perform()
          time.sleep(1)
          row = driver.find_element_by_xpath("(//tr[@role='row' and(@class)])[" + str(i) + "]")
          name = row.find_element_by_xpath(".//div[@class='tweet-name']").text
          content = row.find_element_by_xpath(".//td[2]").text
          date = row.find_element_by_xpath(".//td[2]").text
          impressions = row.find_element_by_xpath(".//td[6]").text
      

      要使用actions,您需要导入

      from selenium.webdriver.common.action_chains import ActionChains
      

      【讨论】:

      • 好的,我添加了单独查找每一行并在获取内容之前滚动到它。我希望现在它会更好地工作
      • 好的,我添加了日期、内容等
      • 不完全确定。再次将元素滚动到视图中后,我添加了row = driver.find_element_by_xpath("(//tr[@role='row' and(@class)])[" + str(i) + "]")。希望这会让它变得更好。
      • 不客气!你看到 Cruisepandey 提到的stale element reference 问题了吗?
      • 太棒了!谢谢!
      【解决方案4】:

      您需要使用find_elements_by_xpath 而不是find_element_by_xpath

      rows = driver.find_elements_by_xpath('//table[@id='tweets_table']//tr//td')
      
      for row in rows:
          print(row.text)
      

      【讨论】:

      • 它正在工作,但仅打印 2 条推文并在其后打印空格。我试过 print(row.text) 而不是 print(row)
      • 你能在<tr>标签下添加HTML
      • 抱歉我没听懂?我应该把它放在哪里?
      • 现在可以试试吗?
      • 您是否要打印用户名和推文内容?还是只推文内容?
      猜你喜欢
      • 2022-12-12
      • 1970-01-01
      • 2023-03-06
      • 2021-12-07
      • 1970-01-01
      • 2013-09-01
      • 2015-11-28
      • 2017-08-27
      • 2018-10-10
      相关资源
      最近更新 更多