【问题标题】:How to get control and pass the values in textbox in a webpage automatically如何自动控制并传递网页文本框中的值
【发布时间】:2021-11-17 19:57:34
【问题描述】:

我想自动加载 url 并获取登录按钮的控制权并在 Visual Studio 中使用 c# 将值传递给文本。我尝试使用 htmlagilitypack 获取控件并在文本框中传递值,我有使用硒网络驱动程序。我能够加载 url 并且无法获得登录按钮的控制并且无法在文本框中传递值。在这种情况下,有人可以帮助我吗?我在过去一周被困住了。我附上了我尝试过的示例代码。

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("xyz@gmail.com");  

【问题讨论】:

    标签: c# selenium-webdriver


    【解决方案1】:

    编辑

    您的按钮在很多 shadowRoot 中:您必须执行此功能才能点击登录按钮:

    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("xyz@gmail.com");
            wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("usernameSubmitButton"))).Click();
    
            wait.Until(ExpectedConditions.ElementExists(By.Id("username"))).SendKeys("email@toto.fr");
            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();
    

    在执行此代码之前,等待 3 或 5 秒以确保页面 html 已完全加载

    所以我没有 EDGE,我用 firefox 测试过

    【讨论】:

    • 嗨,谢谢,但我需要它使用 c# 我尝试了这段代码,但这显示了一个错误,我使用了 IWebElement ele = driver.FindElement(By.Name("dps-button-label") );但它没有捕获登录按钮并单击它,在这种情况下我可以使用 c# 得到答案吗?
    • 它是 C#,我已经修复了一些拼写错误
    • 我做了一个小视频来看看结果drive.google.com/file/d/1q8y5_WcJ830k-JFgcgRcbh3PuwLRZNES/…
    • 非常感谢,能够获得登录控件并将电子邮件 ID 值传递给文本,但无法传递密码字段的值并单击登录按钮最后我尝试了此代码,但它显示(元素不可交互)异常,我附上了我尝试过的代码,在这种情况下有人可以帮助我。 (IWebElement pas1 = driver.FindElement(By.Id("password")); pas1.SendKeys("xxxxx"); IWebElement ele3 = driver.FindElement(By.Id("submitButton")); ele3.Click(); )
    • 对不起,您的问题是在授权电子邮件/密码之后对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 2015-04-01
    • 2019-03-27
    相关资源
    最近更新 更多