我无法使用selenium、python获取网页中应用内的元素(WebScrapping)

hi3rlvi2  于 2022-12-18  发布在  Python
关注(0)|答案(1)|浏览(146)

无论我试图在https://estrelabet.com/ptb/games/detail/casino/demo/7787的飞行员游戏中找到什么,它总是会返回
selenium.common.exceptions.NoSuchElementException:消息:没有此元素:找不到元素:{“方法”:“css选择器”,“选择器”:“.number字体系列编号”}
我正在尝试获取the results中的所有内容
它们都是类为“payouts-block”的div的子级
我甚至可以登录到网站进入游戏,但我不能从里面得到任何元素

# navegador.find_element(By.CLASS_NAME,'payouts-block').text
# navegador.find_element(By.XPATH,'/html/body/app-root/app-game/div/div[1]/div[2]/div/div[2]/div[1]/app-stats-widget/div/div[1]/div')
# /html/body/app-root/app-game/div/div[1]/div[2]/div/div[2]/div[1]/app-stats-widget/div/div[1]/div
# navegador.find_element(By.CLASS_NAME,'amount font-family-number')
# WebDriverWait(navegador,15).until(EC.element_to_be_clickable('number font-family-number'))

到目前为止,这些都是我尝试过的函数,总是出现相同的错误

xuo3flqw

xuo3flqw1#

问题是您要查找的元素在iframe中,因此您需要切换到iframe才能使用它们。
代码工作:

# Needed libs
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver

# Initiate the ddriver and navigate
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://estrelabet.com/ptb/games/detail/casino/demo/7787')

# We save the iframe where the elements you want to get are located
iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "iframeDefaultSize")))

# We switch to that iframe
driver.switch_to.frame(iframe)

# Once we switched to the iframe, we can get the elements you wanted
blocks = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "(//div[@class='payouts-block'])[1]//app-payout-item/div")))

# For every element you show the text
for block in blocks:
    print(block.get_attribute('textContent'))

如果在使用iframe之后,你想使用iframe之外的其他元素,你需要回到主框架,你可以这样做:

driver.switch_to.default_content();

相关问题