Selenium | How to iterate | TypeError: 'WebElement' object is not iterable
Selenium | How to iterate | TypeError: 'WebElement' object is not iterable
请在下面找到我要获取行的附加屏幕截图,即所有推文行。
我正在努力研究如何迭代和获取所有行。
下面的代码我试过了。
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")
要获取所有推文行,您可以使用此
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
您需要使用 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)
这个异常:
类型错误:'WebElement'对象不可迭代
是因为你应该使用 find_elements
其中 returns 一个列表,并且可以迭代,而不是 find_element
其中 returns 一个单一的网络元素。
代码:
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()
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
请在下面找到我要获取行的附加屏幕截图,即所有推文行。 我正在努力研究如何迭代和获取所有行。
下面的代码我试过了。
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")
要获取所有推文行,您可以使用此
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
您需要使用 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)
这个异常:
类型错误:'WebElement'对象不可迭代
是因为你应该使用 find_elements
其中 returns 一个列表,并且可以迭代,而不是 find_element
其中 returns 一个单一的网络元素。
代码:
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()
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