使用Puppeteer-Sharp点击含name属性按钮失败的技术求助
Hey there, let's get that login button click working properly! The issue you're hitting is that name=login-submit isn't a valid CSS selector—Puppeteer uses standard CSS selectors under the hood with querySelector, so we need to adjust the syntax to target the button's name property correctly.
The Correct Selector Syntax
To select an element by its name attribute in CSS, use the attribute selector format:
[name="login-submit"]
This tells the browser to find any element where the name attribute exactly matches login-submit.
Modified Code
Here's your updated code with the fixed selector:
using (Page page = await Browser.NewPageAsync()) { await page.GoToAsync("https://www.oddsportal.com/login/"); await page.TypeAsync("#login-username1", "sfarzoso"); await page.TypeAsync("#login-password1", "password"); // Use the valid attribute selector here await page.ClickAsync("[name='login-submit']"); await page.WaitForNavigationAsync(); var c = await page.GetContentAsync(); }
Why This Works
The original name=login-submit syntax isn't recognized by querySelector—it expects proper CSS syntax, which for attribute matching requires wrapping the attribute and value in square brackets. Since your button has a unique name attribute, this selector will reliably target only that login submit button.
Alternative Options (If Needed)
If for some reason the name selector doesn't work (though it should!), you could also target the button by its class:
await page.ClickAsync(".inline-btn-2");
Just make sure there are no other elements on the page with the same inline-btn-2 class—otherwise, this might click the wrong element. The name attribute selector is safer here since it's unique to the login submit action.
内容的提问来源于stack exchange,提问作者sfarzoso

