有没有办法双击铬硒C#

时间:2018-07-11 12:37:44

标签: c# selenium selenium-webdriver

我正在编写一个测试脚本。我的测试在某个点上失败了,因为需要单击两次按钮。

我已经临时更改了代码,因此它两次包含同一段代码,这可以解决问题。但是有没有办法引入双击呢?

代码:

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

这是完整的代码:

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();

2 个答案:

答案 0 :(得分:0)

您可以参考

使用操作类:

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

使用JavaScriptExecutor:

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

答案 1 :(得分:0)

您可以尝试使用此代码。您要做的只是向下滚动到特定元素(在您的情况下为继续按钮),而不是double click

您可以尝试使用的代码:

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("L.dockster@stackoverflow.com");
((IJavascriptExecutor) webDriver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 700)");
webDriver.findElement(By.XPath("//button[text()='Continue']")).Click();