无法使用Selenium从网页获取文本



我只是想从主页上抓取所有的文本。这段代码不返回任何东西。最终目标是扫描页面的衬衫图标出现的时间。如果匹配成功。提前感谢:

附上的第一张图片是一个匹配的html,我想通过所有的页面上循环附件中的第二张图片是出现的衬衫图标的html

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import time
PATH = "C:Program Files (x86)chromedriver.exe"
chrome_options = Options()
chrome_options.add_argument(" - incognito")
driver = webdriver.Chrome(PATH,options=chrome_options)
driver.get("https://www.flashscore.com/")
driver.implicitly_wait(5)
try:
main = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "sportName soccer"))
)
print(main.text)
except:
print("No Text Found")
driver.quit()

获取该元素中的所有文本:

main = driver.find_element_by_xpath("//div[contains(@class,'sportName soccer')]")
print(main.text)

只获取事件

listofevents = driver.find_elements_by_xpath("//div[contains(@class,'event__match event__match--oneLine event__match--scheduled')]")
for i in listofevents:
print(i.text)

只需将定位器策略从使用类更改为使用xpath。

try:
main = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//div[@id="live-table"]/div[@class="event"]/div[@class="leagues--live "]/div[@class="sportName soccer"]')))
print(main.text)
except:
print("No Text Found")
driver.quit()

最新更新