当按钮出现在网页上时,我想以编程方式单击它,有办法吗?
这是按钮,它没有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()");
变量脚本的字符串具有getElementsByClassName
,并使用索引0处的结果(看起来是正确的(,但是实际运行的脚本有getElementByClassName
,单数,它不存在(当然也没有索引器(。。你试着运行脚本变量了吗?
string script = @"document.getElementsByClassName('VotingButton VotingButton--upvote btn-white')[0].click();"
browser.ExecuteScriptAsync(script);
除了cefsharp代码之外,它可以执行所有操作,但如果javascript中有类似的内容,script
中的代码应该通过cefsharl。。。
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>
您可以使用硒、
Nuget进口硒。RC,硒。支持,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();
}