如何捕获从 Selenium Web 驱动程序返回的值并将其存储为一个列表
我是编程的新手,我正在使用Selenium从FlashScore的计划匹配中获取数据,我打算将每个循环返回的每个列表分配到一个变量中,以便我可以执行一些计算并弹出我不需要的内容。
Selenium Web驱动程序在每个循环中返回2个元素,并且两个元素在不同的列表上,这使我很难访问或进行计算
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Firefox()
url = 'https://www.flashscore.com/'
driver.get(url)
time.sleep(2)
#closing Accept terms
element = driver.find_element(By.XPATH, '//*[@id="onetrust-accept-btn-handler"]').click()
#clicking on Scheduled matches
Scheduled = driver.find_element(By.XPATH, '/html/body/div[7]/div[1]/div/div[1]/div[2]/div[4]\
/div[2]/div/div[1]/div[1]/div[5]/div').click()
time.sleep(2)
# getting all Scheduled matchs
try:
#finding scheduled matches
Scheduled = driver.find_elements(By.XPATH, "//*[starts-with(@id, 'g_1')]")
time.sleep(3)
except NoSuchElementException:
pass
for value in Scheduled:
window_home = driver.window_handles[0]
value.click()
time.sleep(3)
window_1_open = driver.window_handles[1]
driver.switch_to.window(window_1_open)
driver.maximize_window()
time.sleep(2)
try:
time.sleep(3)
find_standing = driver.find_element(By.LINK_TEXT, 'STANDINGS')
time.sleep(2)
find_standing.click()
time.sleep(3)
get_match_values = driver.find_elements(By.XPATH,"//*[starts-with(@class, \
'ui-table__row table__row--selected')]")
time.sleep(2)
for values in get_match_values:
print(values.text)
driver.close()
driver.switch_to.window(window_home)
except NoSuchElementException:
driver.close()
driver.switch_to.window(window_home)
I'm new to programming and I am using selenium to get data from scheduled matches from flashscore, I intend to assign each list returned from each loop to a variable so I can perform some calculations and also pop what I don't need.
Selenium web driver returns 2 elements on each for loop and the two elements are on different lists which makes it hard for me to access it or make the calculation
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Firefox()
url = 'https://www.flashscore.com/'
driver.get(url)
time.sleep(2)
#closing Accept terms
element = driver.find_element(By.XPATH, '//*[@id="onetrust-accept-btn-handler"]').click()
#clicking on Scheduled matches
Scheduled = driver.find_element(By.XPATH, '/html/body/div[7]/div[1]/div/div[1]/div[2]/div[4]\
/div[2]/div/div[1]/div[1]/div[5]/div').click()
time.sleep(2)
# getting all Scheduled matchs
try:
#finding scheduled matches
Scheduled = driver.find_elements(By.XPATH, "//*[starts-with(@id, 'g_1')]")
time.sleep(3)
except NoSuchElementException:
pass
for value in Scheduled:
window_home = driver.window_handles[0]
value.click()
time.sleep(3)
window_1_open = driver.window_handles[1]
driver.switch_to.window(window_1_open)
driver.maximize_window()
time.sleep(2)
try:
time.sleep(3)
find_standing = driver.find_element(By.LINK_TEXT, 'STANDINGS')
time.sleep(2)
find_standing.click()
time.sleep(3)
get_match_values = driver.find_elements(By.XPATH,"//*[starts-with(@class, \
'ui-table__row table__row--selected')]")
time.sleep(2)
for values in get_match_values:
print(values.text)
driver.close()
driver.switch_to.window(window_home)
except NoSuchElementException:
driver.close()
driver.switch_to.window(window_home)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过使用索引来捕获每个字符串来解决它。
I was able to resolve it by using index to catch each of the strings.