L.Dockster
L.Dockster

Reputation: 199

Is there a way to double click on chrome selenium c#

I'm writing a test script. My test is failing at a certain point as the button needs to be clicked twice.

I've temporarily changed my code so it includes the same piece of code twice and that does the trick. But is there a way of introducing a double click instead?

Code :

webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();
webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();

This is full code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Exercise1
{
    class RunPath
    {

            static void Main()
            {

                IWebDriver webDriver = new ChromeDriver();
                webDriver.Navigate().GoToUrl("https://energy.gocompare.com/gas-electricity");
                webDriver.Manage().Window.Maximize();
            String title = webDriver.Title;
            String expectedTitle = "Utilities from Go Compare";
            if (title.Contains(expectedTitle))
            {
                Console.WriteLine("Tile is matching with expected value");
            }
            else
            {
                Console.WriteLine("Tile is matching with expected value");
            }
            webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();
            webDriver.FindElement(By.XPath(".//input[@type = 'text']")).SendKeys("W30PN#");
            webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();
            webDriver.FindElement(By.XPath(".//input[@type = 'text']")).Clear();
            webDriver.FindElement(By.XPath(".//input[@type = 'text']")).SendKeys("W30PN");
            webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();
            webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();

Upvotes: 4

Views: 3468

Answers (2)

cruisepandey
cruisepandey

Reputation: 29382

You can try this code. All you need to do is scroll down to the specific element (in your case it is continue button) instead of double click.

Code you can try out :

webDriver.Navigate().GoToUrl("https://energy.gocompare.com/gas-electricity");
IWebElement postcode =  
webDriver.FindElement(By.CssSelector("input[placeholder='Postcode']"));
postcode.Clear();
postcode.SendKeys("B33 8TH");
IWebElement email =  webDriver.FindElement(By.CssSelector("input[placeholder='Email (Optional)']"));
email.Clear();
email.SendKeys("[email protected]");
((IJavascriptExecutor) webDriver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 700)");
webDriver.findElement(By.XPath("//button[text()='Continue']")).Click();

Upvotes: 0

Ishita Shah
Ishita Shah

Reputation: 4035

You may Refer this,

Using Action Class:

    Actions action  = new Actions(driver);
    action.MoveToElement("Web Element To Click").DoubleClick().Perform();

Using JavaScriptExecutor:

((JavascriptExecutor)driver).ExecuteScript("arguments[0].dblclick();", "Element to Click");

Upvotes: 1

Related Questions