c# Selenium form automation problem sendkeys random

时光怂恿深爱的人放手 提交于 2020-08-10 19:20:07

问题


I'm trying to make a bot that can fill a form with random values from the login form in the Bidoo site. I am able to send keys with .SendKeys("keys") but I'm not able to send them randomly. I've already made a working random string generator but I'm not able to use it with the auto form filler (the "Bot" that I'm trying to make). I've tried using IWebDriver and IWebElement but when I start the "Bot" it says that it can't find the XPath (if I try to use the CSS selector or the ID or the Class I've got the same error). Thank you for your help!

IMPORTANT: I DON'T HAVE ANY C# EXPERIENCE AT ALL!

I also have this problem so thank you if you'll answer: Repeat a task once it's finished

Bidoo BOT UI (button3 is used to generate the random string)

This is the SendKeys() code (the working code):

using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Interactions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BidooBOT
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

//this is the bot
        private void button2_Click(object sender, EventArgs e)
        {
            if (radioChrome.Checked == true)
            {
                var driverchrome = new ChromeDriver();

                driverchrome.Navigate().GoToUrl(textBox1.Text);

                driverchrome.FindElementByXPath("//*[@id='email_signup']").SendKeys("email@gmail.com");

                driverchrome.FindElementByXPath("//*[@id='holdon']/div[1]/div/form/div[2]/input").SendKeys("password");

                driverchrome.FindElementByXPath("//*[@id='password_signup']").SendKeys("password");

                driverchrome.FindElementByXPath("//*[@id='holdon']/div[1]/div/form/div[4]/div/label/input").Click();

                driverchrome.FindElementByXPath("//*[@id='btnRegister']").Click();
            }

            else if (radioFirefox.Checked == true)
            {
                var driverfirefox = new FirefoxDriver();

                driverfirefox.Navigate().GoToUrl(textBox1.Text);

                driverfirefox.FindElementByXPath("//*[@id='email_signup']").SendKeys("email@gmail.com");

                driverfirefox.FindElementByXPath("//*[@id='holdon']/div[1]/div/form/div[2]/input").SendKeys("username");

                driverfirefox.FindElementByXPath("//*[@id='password_signup']").SendKeys("password");

                driverfirefox.FindElementByXPath("//*[@id='holdon']/div[1]/div/form/div[4]/div/label/input").Click();

                driverfirefox.FindElementByXPath("//*[@id='btnRegister']").Click();
            }
        }

//this is the random string generator (working)
        private void button3_Click(object sender, EventArgs e)
        {
            char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray();
            Random random = new Random();
            string randomString = "";
            for (int i = 0; i < 10; i++)
            {
                randomString += letters[random.Next(0, 60)].ToString();
            }
            MessageBox.Show(randomString);
        }
    }
} ```

来源:https://stackoverflow.com/questions/62876292/c-sharp-selenium-form-automation-problem-sendkeys-random

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!