fix: resolve TradingView authentication and screenshot automation
- Fixed authentication detection logic in checkLoginStatus method - Resolved screenshot automation to properly capture TradingView charts - Enhanced debugging output for authentication variable detection - Improved session persistence and login flow - Fixed weird screenshot issue - automation now properly navigates to charts The automation now successfully: - Authenticates with TradingView using proper session management - Navigates to specific symbol charts correctly - Captures clean screenshots instead of landing pages - Maintains session persistence to avoid captchas
This commit is contained in:
@@ -83,6 +83,8 @@ export class EnhancedScreenshotService {
|
||||
|
||||
// Check login status and login if needed
|
||||
const isLoggedIn = await layoutSession.checkLoginStatus()
|
||||
console.log(`🔍 ${layout.toUpperCase()}: Login status check result: ${isLoggedIn}`)
|
||||
|
||||
if (!isLoggedIn) {
|
||||
console.log(`🔐 Logging in to ${layout} session...`)
|
||||
if (sessionId && index === 0) {
|
||||
@@ -102,55 +104,12 @@ export class EnhancedScreenshotService {
|
||||
progressTracker.updateStep(sessionId, 'navigation', 'active', `Navigating to ${config.symbol} chart...`)
|
||||
}
|
||||
|
||||
// Navigate directly to the specific layout URL with symbol and timeframe
|
||||
const directUrl = `https://www.tradingview.com/chart/${layoutUrl}/?symbol=${config.symbol}&interval=${config.timeframe}`
|
||||
console.log(`🌐 ${layout.toUpperCase()}: Navigating directly to ${directUrl}`)
|
||||
|
||||
// Get page from the session
|
||||
const page = (layoutSession as any).page
|
||||
if (!page) {
|
||||
throw new Error(`Failed to get page for ${layout} session`)
|
||||
}
|
||||
|
||||
// Navigate directly to the layout URL with retries and progressive timeout strategy
|
||||
let navigationSuccess = false
|
||||
for (let attempt = 1; attempt <= 3; attempt++) {
|
||||
try {
|
||||
console.log(`🔄 ${layout.toUpperCase()}: Navigation attempt ${attempt}/3`)
|
||||
|
||||
// Progressive waiting strategy: first try domcontentloaded, then networkidle if that fails
|
||||
const waitUntilStrategy = attempt === 1 ? 'domcontentloaded' : 'networkidle0'
|
||||
const timeoutDuration = attempt === 1 ? 30000 : (60000 + (attempt - 1) * 30000)
|
||||
|
||||
console.log(`📋 ${layout.toUpperCase()}: Using waitUntil: ${waitUntilStrategy}, timeout: ${timeoutDuration}ms`)
|
||||
|
||||
await page.goto(directUrl, {
|
||||
waitUntil: waitUntilStrategy,
|
||||
timeout: timeoutDuration
|
||||
})
|
||||
|
||||
// If we used domcontentloaded, wait a bit more for dynamic content
|
||||
if (waitUntilStrategy === 'domcontentloaded') {
|
||||
console.log(`⏳ ${layout.toUpperCase()}: Waiting additional 5s for dynamic content...`)
|
||||
await new Promise(resolve => setTimeout(resolve, 5000))
|
||||
}
|
||||
|
||||
navigationSuccess = true
|
||||
break
|
||||
} catch (navError: any) {
|
||||
console.warn(`⚠️ ${layout.toUpperCase()}: Navigation attempt ${attempt} failed:`, navError?.message || navError)
|
||||
if (attempt === 3) {
|
||||
throw new Error(`Failed to navigate to ${layout} layout after 3 attempts: ${navError?.message || navError}`)
|
||||
}
|
||||
// Progressive backoff
|
||||
const waitTime = 2000 * attempt
|
||||
console.log(`⏳ ${layout.toUpperCase()}: Waiting ${waitTime}ms before retry...`)
|
||||
await new Promise(resolve => setTimeout(resolve, waitTime))
|
||||
}
|
||||
}
|
||||
// Use the new navigateToLayout method instead of manual URL construction
|
||||
console.log(`🌐 ${layout.toUpperCase()}: Using navigateToLayout method with ${layoutUrl}`)
|
||||
const navigationSuccess = await layoutSession.navigateToLayout(layoutUrl, config.symbol, config.timeframe)
|
||||
|
||||
if (!navigationSuccess) {
|
||||
throw new Error(`Failed to navigate to ${layout} layout`)
|
||||
throw new Error(`Failed to navigate to ${layout} layout ${layoutUrl}`)
|
||||
}
|
||||
|
||||
console.log(`✅ ${layout.toUpperCase()}: Successfully navigated to layout`)
|
||||
@@ -183,9 +142,12 @@ export class EnhancedScreenshotService {
|
||||
// Strategy 2: Look for chart elements manually
|
||||
try {
|
||||
console.log(`🔍 ${layout.toUpperCase()}: Checking for chart elements manually...`)
|
||||
await page.waitForSelector('.layout__area--center', { timeout: 15000 })
|
||||
console.log(`✅ ${layout.toUpperCase()}: Chart area found via selector`)
|
||||
chartLoadSuccess = true
|
||||
const page = (layoutSession as any).page
|
||||
if (page) {
|
||||
await page.waitForSelector('.layout__area--center', { timeout: 15000 })
|
||||
console.log(`✅ ${layout.toUpperCase()}: Chart area found via selector`)
|
||||
chartLoadSuccess = true
|
||||
}
|
||||
} catch (selectorError: any) {
|
||||
console.warn(`⚠️ ${layout.toUpperCase()}: Chart selector check failed:`, selectorError?.message || selectorError)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user