Home > other >  How to get control and pass the values in textbox in a webpage automatically
How to get control and pass the values in textbox in a webpage automatically

Time:11-18

i wants to automatically load the url and get the control of signin button and pass the value to the text using c# in visual studio.i have tried using htmlagilitypack for getting the control and for passing the values in a textbox, i have used selenium webdriver. iam able to load the url and not able to get the control of signin button and not able to pass the value in textbox. could someone help me in this case i was stuck with past one week.i have attached the sample code i have tried.

using System;
using HtmlAgilityPack;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using System.Threading;
using System.Threading.Tasks;   

namespace HtmlAgilityPackTest
{​​​​​
    class Program
    {​​​​​
        static void Main(string[] args)
        {​​​​​
            //string driver = "Email";
            string itext = "Sign In";
            //string email = "Email";
            //IWebDriver driver = new EdgeDriver();
            var uri = "https://developer.servicenow.com";
            var psi = new System.Diagnostics.ProcessStartInfo();
            psi.UseShellExecute = true;
            psi.FileName = uri;

            System.Diagnostics.Process.Start(psi);
            //Thread.Sleep(3000);
            var func = string.Format(@"document.getElementsByClass('dps-button-label').innerText = '{​​​​​0}​​​​​';",itext);

            //Thread.Sleep(3000);
                IWebDriver driver = new EdgeDriver();
                IWebElement element = driver.FindElement(By.Name("form-control input-box username-box"));

                element.SendKeys("[email protected]");  

CodePudding user response:

EDIT

your button is in lot shadowRoot: you have to do this function to click on signin button:

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;

:
:
:
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));

        driver.Manage().Window.Maximize();
        driver.Url = "https://developer.servicenow.com";
        Thread.Sleep(3000);
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        var query = "return document.querySelector('dps-app').shadowRoot";
        query  = ".querySelector('dps-navigation-header').shadowRoot";
        query  = ".querySelector('dps-login').shadowRoot";
        query  = ".querySelector('dps-button')";
        IWebElement elt = (IWebElement)js.ExecuteScript(query);
        elt.Click();
           
        # you have to install SeleniumExtras.WaitHelpers nugget package fo that
        wait.Until(ExpectedConditions.ElementExists(By.Id("username"))).SendKeys("[email protected]");
        wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("usernameSubmitButton"))).Click();

        wait.Until(ExpectedConditions.ElementExists(By.Id("username"))).SendKeys("[email protected]");
        wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("usernameSubmitButton"))).Click();
        var but = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("submitButton")));
        driver.FindElement(By.Id("password")).SendKeys("password");
        but.Click();

before executing this code, wait 3 or 5 sec to be sure the page html is loaded completely

so i havent EDGE, i have tested with firefox

  • Related