简体   繁体   English

如何使用 selenium c# 单击按钮

[英]How to click button using selenium c#

<button class="btn btn-outline-primary btn-lg" id="btnPermission">GO</button>

The above codes are from a website, however I am unable to click this button despite trying multiple attempts.上面的代码来自一个网站,但是尽管尝试了多次,我还是无法点击这个按钮。 What should I do?我应该怎么办?

 IWebElement reklamgec3 = driver.FindElement(By.CssSelector("#btnPermission"));
 ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", reklamgec3);



IWebElement reklamgec3 = driver.FindElement(By.ClassName("btn-outline-primary"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", reklamgec3)


IWebElement reklamgec3 = driver.FindElement(By.XPath("//*[@id="btnPermission"]"));
   ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", reklamgec3)


 IWebElement reklamgec3 = driver.FindElement(By.Id("btnPermission"));
  ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", reklamgec3)

      IWebElement reklamgec3 = driver.FindElement(By.CssSelector("#btnPermission"));
        reklamgec3.Click();

How to click button using c# selenium.如何使用 c# selenium 单击按钮。 The above does not work for me.以上对我不起作用。

You need to use an explicit wait :您需要使用显式等待

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));

wait.Until(d => d.FindElement(By.Id("btnPermission")).Click());

Waiting for the existence of a element is frequently not enough.等待元素的存在通常是不够的。 You need to wait for it to be clickable.您需要等待它可以点击。 Many times that just means attempting to find then click the element until the click event succeeds.很多时候,这只是意味着尝试查找然后单击元素,直到单击事件成功。

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

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