2

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.

1

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! It worked. But I couldn't understand the expression clearly. Is it an expression of XPath?
Which of them? There are two there (well... actually none of them is xpath)
Attribute selectors look like XPath predicates but are otherwise unrelated.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.