Delphi TChromium组件自动化:表单操作后遇新问题求助
Hey there! It sounds like you had a reliable setup with Delphi and TChromium for automating form fills and submissions using ExecuteJavaScript, but now you're hitting a roadblock. Since you didn’t specify the exact issue you’re facing, let’s walk through the most common snags and fixes that pop up with this kind of automation:
Common Issues & Solutions
1. Element Not Found Errors
If your JavaScript throws an error that the LoginForm_username element doesn’t exist, check these things:
- Wait for the page to fully load before running your script. TChromium might execute your code before the DOM is ready. Use the
OnLoadEndevent to trigger automation only after the main frame finishes loading:procedure TForm1.Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer); begin if frame.IsMain then begin // Run your form fill/submit code here Chromium1.browser.MainFrame.ExecuteJavaScript('document.getElementById(''LoginForm_username'').value="' + Ini.ReadString('config', 'usuario', '') + '"', frame.GetURL, 0); Chromium1.Browser.MainFrame.ExecuteJavaScript('document.forms[0].submit()', frame.GetURL, 0); end; end; - Verify the element’s ID hasn’t changed on the target page. Inspect the page source to confirm the ID is still
LoginForm_username.
2. Form Submission Fails to Trigger
If the form fills correctly but won’t submit, try these adjustments:
- Target the form directly by its ID (if it has one) instead of using
document.forms[0]:document.getElementById('LoginForm').submit() - Some forms require a real button click to trigger validation scripts. Simulate a click on the submit button instead of calling
submit():document.getElementById('submitButton').click()
3. JavaScript Execution Context Problems
Always ensure you’re running scripts in the main frame. Frames or iframes can cause context mismatches. Double-check that frame.IsMain is true before executing your code, as shown in the OnLoadEnd example above.
4. Special Character Escaping Issues
If your username from the INI file contains quotes, backslashes, or other special characters, it will break the JavaScript string. Use an escape function to sanitize the value first:
function EscapeJSString(const S: string): string; begin Result := StringReplace(S, '\', '\\', [rfReplaceAll]); Result := StringReplace(Result, '''', '\''', [rfReplaceAll]); Result := StringReplace(Result, '"', '\"', [rfReplaceAll]); end; // Usage in your script: Chromium1.browser.MainFrame.ExecuteJavaScript('document.getElementById(''LoginForm_username'').value="' + EscapeJSString(Ini.ReadString('config', 'usuario', '')) + '"', frame.GetURL, 0);
5. Security Restrictions Blocking Scripts
If the target page updated its security settings, it might block your automation. For testing purposes, you can disable web security in TChromium (note: avoid this in production):
procedure TForm1.FormCreate(Sender: TObject); var Settings: TCefSettings; begin Settings := TCefSettings.Create; Settings.CefCommandLineArgs.Add('disable-web-security', ''); Settings.CefCommandLineArgs.Add('allow-running-insecure-content', ''); if not Chromium1.Initialize(Settings) then ShowMessage('Failed to initialize Chromium'); Settings.Free; end;
If you can share more details about the specific error or behavior you’re seeing (like error messages, whether the form fills but doesn’t submit, etc.), I can help narrow it down further!
内容的提问来源于stack exchange,提问作者Edwin Mejias

