简体   繁体   English

如何执行点击网页上的按钮?

[英]How to perform click on button on web page?

I'd like to click programmatically on the button when it appears on the web page, is there a way to do it?当按钮出现在网页上时,我想以编程方式单击它,有没有办法做到这一点?

here is the button, it has no ID, only this;这是按钮,它没有 ID,只有这个;

<div class="VotingButtons"><button class="VotingButton VotingButton--upvote btn-white" type="button">Like</button>

Here are more information about the button:以下是有关该按钮的更多信息:

<div class="VotingWrapper VotingWrapper--isBlocking"><p class="VotingWrapper__text"><strong>Do you like this image?</strong></p><div class="VotingButtons"><button class="VotingButton VotingButton--upvote btn-white" type="button">Like</button><button class="VotingButton VotingButton--downvote btn-white" type="button"><span class="VotingButton__text--hideOnMobile">Dislike</span></button></div></div>

I tried to use this code but it doesn't work:我尝试使用此代码但它不起作用:

private async void Btn_GoToDirectly_Click(object sender, EventArgs e)
    {
        
        var script = @"
document.getElementsByClassName('VotingButton VotingButton--upvote btn-white')[0].click();
browser.ExecuteScriptAsync(script);
    ";
            browser.ExecuteScriptAsync("document.getElementByClassName('VotingButton VotingButton--upvote btn-white').click()");

the string you have for the variable script has getElementsByClassName and uses the result at index 0 (seems correct), but the script you actually run has getElementByClassName , singular, which doesn't exist (and no indexer of course).. did you try to run the script variable?您用于变量脚本的字符串具有getElementsByClassName并使用索引 0 处的结果(似乎正确),但您实际运行的脚本具有getElementByClassName ,单数,它不存在(当然也没有索引器).. 你试过吗?运行脚本变量?

    string script = @"document.getElementsByClassName('VotingButton VotingButton--upvote btn-white')[0].click();"
    browser.ExecuteScriptAsync(script);

this does everything but the cefsharp code- but if you have something like this in javascript, the code in script should work through cefsharp...除了cefsharp代码之外,这可以完成所有事情-但是如果您在javascript中有这样的东西, script的代码应该通过cefsharp工作...

 let like= function(){ console.log("like"); }; let trigger= function(){ console.log("trigger"); document.getElementsByClassName('VotingButton VotingButton--upvote btn-white')[0].click(); };
 <html> <body> <div class="VotingWrapper VotingWrapper--isBlocking"> <p class="VotingWrapper__text"> <strong>Do you like this image?</strong> </p> <div class="VotingButtons"> <button onclick="like()" class="VotingButton VotingButton--upvote btn-white" type="button">Like</button> <button class="VotingButton VotingButton--downvote btn-white" type="button"> <span class="VotingButton__text--hideOnMobile">Dislike</span> </button> </div> </div> <button onclick="trigger()" type="button">Trigger</button> </body> </html>

you can use selenium,你可以使用硒,

Nuget import Selenium.RC,Selenium.Support,Selenium.WebDriver, Nuget 导入 Selenium.RC,Selenium.Support,Selenium.WebDriver,

 using (IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver())
     {
         driver.Navigate().GoToUrl("http://www.xxurl.com");  
         driver.FindElements(By.ClassName("VotingButton VotingButton--upvote btn-white")).Click();
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM