使用Selenium自动化Plus500登录遇TimeoutException问题求助
Hey there, let's break down why you're hitting that TimeoutException and how to fix it. Plus500's login page has a few common gotchas that trip up Selenium scripts—here's what to check:
1. You're Not Switching to the New Window
When you use window.open() to launch the login page, Selenium stays focused on the original browser window by default. That means your script is trying to find elements in the wrong context entirely!
Fix it with this code right after opening the new window:
# Grab all open window handles window_handles = driver.window_handles # Switch to the last window (the one you just opened) driver.switch_to.window(window_handles[-1])
2. The Login Form Might Be Inside an Iframe
Many trading platforms like Plus500 load login modules inside iframes to isolate content. If you try to locate elements directly without switching to the iframe first, Selenium will never find them.
Check and switch to the iframe:
- Open your browser's dev tools and see if the login inputs are wrapped in an
<iframe>tag. - If yes, add this before your element locators:
# Wait for the iframe to load and switch into it (adjust the locator to match your page) WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@src, 'login')]"))) - After interacting with the login form, switch back to the main page if needed:
driver.switch_to.default_content()
3. Anti-Bot Measures Are Blocking You
Plus500 actively detects automated tools like Selenium. Default Selenium settings have telltale signs that flag your script as a bot, leading to slow loading or hidden elements.
Try these tweaks to bypass detection:
- Maximize the browser window to mimic real user behavior:
options = webdriver.ChromeOptions() options.add_argument("--start-maximized") driver = webdriver.Chrome(options=options) - Hide Selenium's automation markers:
options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) - Add small user-like actions, like moving the mouse:
from selenium.webdriver.common.action_chains import ActionChains ActionChains(driver).move_by_offset(50, 50).perform()
4. Your Locators Might Be Unreliable (Less Likely, But Worth Checking)
While your ID-based XPaths should work, sometimes dynamic page updates can break them. Test more flexible locators just in case:
- Use the
nameattribute instead:(By.NAME, "email") - Use placeholder text:
(By.XPATH, "//input[@placeholder='Email address']")
Full Modified Code Example
Here's how your script should look with all these fixes applied:
from selenium import webdriver 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.common.action_chains import ActionChains # Configure Chrome to avoid bot detection options = webdriver.ChromeOptions() options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) options.add_argument("--start-maximized") driver = webdriver.Chrome(options=options) # Open the login window driver.execute_script("window.open('https://app.plus500.com/trade?refurl=https%3A%2F%2Fwww.google.com%2F&innerTags=_cc_%20exp_new_sas_c&webvisitid=2a3139bc-78f7-4f48-a50d-c79e7b3e7abd&page=login&_ga=2.24870842.1255829517.1590833562-259196075.1590833562', 'new window')") # Switch to the new login window window_handles = driver.window_handles driver.switch_to.window(window_handles[-1]) # Switch to login iframe (adjust locator if needed) try: WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@src, 'login')]"))) except: # No iframe found, proceed normally pass # Simulate mouse movement to look like a real user ActionChains(driver).move_by_offset(50, 50).perform() # Enter credentials and submit WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'email'))).send_keys('myemail') WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'password'))).send_keys('mypassword') WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'submitLogin'))).click() # Switch back to main content if you switched to an iframe earlier # driver.switch_to.default_content()
内容的提问来源于stack exchange,提问作者Lorenzo Mussetti

