[英]Unable to locate element with xpath using python and selenium
[英]Unable to locate element on a webpage using xpath in selenium (python)
我正在嘗試從一個名為: https ://dexscreener.com/ethereum/0x1a89ae3ba4f9a97b10bac6a77061f00bb956858b 的網站獲取一些數據,我正在嘗試獲取元素:/html/body/div[1]/div/main/div/div [2]/div/div[2]/div/div/div[1]/div[4]/div[2]/div[1]/div[1]/div[2]/span[2] 其中基本上是網頁上代表音量的數字。
我在這里使用了這段代碼:
driver.get('https://dexscreener.com/ethereum/' + str(tokenadress))
try:
fivemVolume = WebDriverWait(driver, delay).until(EC.presence_of_element_located(
(By.XPATH, '/html/body/div[1]/div/main/div/div[2]/div/div[2]/div/div/div[1]/div[4]/div[2]/div[1]/div[1]/div[2]/span[2]')))
except:
#more codee
我認為這與默認加載到某些 iframe 的網頁有關,但是當我添加此代碼時它沒有幫助:
driver.switch_to.default_content()
您的定位器與該頁面上的任何元素都不匹配。
您嘗試訪問的元素在 iframe 中。
因此,您首先需要切換到 iframe。
以下代碼應該可以工作,但我在該頁面上運行 Selenium 時遇到問題,因為它被 cloudflare 阻止了:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)
url = "https://dexscreener.com/ethereum/0x1a89ae3ba4f9a97b10bac6a77061f00bb956858b"
driver.get(url)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id*='tradingview']")))
value = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-name='legend-source-item'] [class='valueItem-1WIwNaDF'] .valueValue-1WIwNaDF"))).text
print(value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.