我无法使用Selenium,python(WebScrapping)在网页中的应用程序中获取元素



无论我尝试在飞行员游戏中找到什么https://estrelabet.com/ptb/games/detail/casino/demo/7787它总是返回

selenium.common.exceptions.NoSuchElementException: Message: no such element: cannot to locate element: {"method";css selector";font-family-number"}

我试图得到结果里面的一切
他们都是div的子类'payouts-block'

我甚至可以登录网站进入游戏,但我不能从里面得到任何元素

# 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'))

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

问题是你正在寻找的元素在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之外的其他元素,你需要回到主框架,你可以使用line:

driver.switch_to.default_content();

最新更新