【问题标题】:How to perform click on button on web page?如何在网页上执行点击按钮?
【发布时间】:2020-10-20 00:25:36
【问题描述】:

当按钮出现在网页上时,我想以编程方式点击它,有什么方法吗?

这里是按钮,没有ID,只有这个;

<div class="VotingButtons"><button class="VotingButton VotingButton--upvote btn-white" type="button">Like</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>

我尝试使用此代码,但它不起作用:

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()");

【问题讨论】:

  • 来自tagging help page你应该在标题中使用标签的唯一情况是它们与标题的对话语气有机。用你的格式引用那个页面'已尝试使用:“避免在以下任何格式的标题中插入标签:” “[tag]: [question title]” and “[question标题] -- [标签] [标签] [标签]".
  • 确保您的 JavaScript 在 DevTools 中工作。请参阅github.com/cefsharp/CefSharp/wiki/…,您应该从 JavaScript 中删除 ExecuteScriptAsync。
  • @amaitland 不幸的是,没有...

标签: javascript c# cefsharp chromium-embedded


【解决方案1】:

变量脚本的字符串有 getElementsByClassName 并使用索引 0 处的结果(似乎正确), 但是您实际运行的脚本有 getElementByClassName ,单数,不存在(当然也没有索引器)..您是否尝试运行脚本变量?

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

除了 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>

【讨论】:

  • @audzyy 我试过了,但它不起作用......另外,按钮文本是“喜欢”,我不知道如何定位它
  • 查找可以使用Array.from(document.querySelectorAll('button')) .find(btn =&gt; btn.textContent === 'Like');之类的文本
  • 另外,您是否注意到我修改了您在脚本字符串中发布的内容?所以它在字符串中没有browser.ExecuteScriptAsync 部分
  • 尝试了一切,复制了您的代码和所有内容,但没有运气......
【解决方案2】:

你可以使用硒,

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();
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 2015-03-08
    • 2012-01-28
    • 2019-03-08
    • 2018-12-22
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多