8berry
8berry

Reputation: 61

select and click a button with CSS selector in C# selenium

I want to click a button of a webpage, but I don't know how to select and click a button with Selenium.

The target source is below:

<span class="login-bt">
<a href="#" onclick="return sChangeURL('GB2101_KAIINLOGIN','https://north2.eplus.jp/sys/main.jsp')" >ログイン</a>
</span>

Here is my code.

using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;


class Test
{
static void Main(string[] args)
{
    IWebDriver driver = new FirefoxDriver();
    driver.Navigate().GoToUrl("http://north2.eplus.jp/sys/main.jsp?uji.verb=GGWP01_mousikomi&uji.bean=B.apl.web.JOAB070100Bean&uketsukeInfoKubun=001&ZScreenId=GGWA01&_ga=1.146803575.1900392715.1483976716");
    IWebElement query = driver.FindElement(By.Id("LoginId"));
    query.SendKeys("*******@gmail.com");

    IWebElement query2 = driver.FindElement(By.Id("LoginPassword"));

    query2.SendKeys("******");


   IWebElement a = driver.FindElement(By.CssSelector("onclick=\"return"));

   a.Click();

}
}

I want to know how to select and click the login button. Thank you for reading.

Upvotes: 2

Views: 2103

Answers (2)

MikeS
MikeS

Reputation: 1764

Ideally, for performance reasons, you should probably put an id on the button if you have control over the html that is rendered. Otherwise, keying off of the parent span as Dekel suggested is your best option.

Upvotes: 0

Dekel
Dekel

Reputation: 62536

Your problem is that onclick=\"return is not a css selector.

You can try [onclick~="return"]to find elements that have the onclick attribute with the value starts with return, but I really don't think it's a good idea.

Another option is to target the anchor inside the login-bt class:

span.login-bt a

Upvotes: 2

Related Questions