Quân Nè
Quân Nè

Reputation: 7

Using selemium autofill textbox Name a address forms.office C#

I have link self-declaration daily: https://forms.office.com/Pages/ResponsePage.aspx?id=IWuHvDTxEkyiZY7Sa38PO_KiPmlYCpVOuIi4NlZfBs1UOVlCNlNFUTkyR0IwQVZaVDc2Uk9QUlVBUCQlQCN0PWcu

This is link of mircosoft no virus. I want write applications auto fill Full_Name, Address, Email and click button in forms

with element input below:

<input class="office-form-question-textbox office-form-textfield-input form-control office-form-theme-focus-border border-no-radius" placeholder="Enter your answer" spellcheck="true" maxlength="4000" aria-labelledby="QuestionId_rbf8060578cb4410ba72d3e97840ebd95">

I tried the code in the project:

   var driverService = ChromeDriverService.CreateDefaultService();
            driverService.HideCommandPromptWindow = true;
            ChromeOptions options = new ChromeOptions();
            var driver = new ChromeDriver(driverService, options);
            driver.Navigate().GoToUrl("https://forms.office.com/Pages/ResponsePage.aspx?id=IWuHvDTxEkyiZY7Sa38PO_KiPmlYCpVOuIi4NlZfBs1UOVlCNlNFUTkyR0IwQVZaVDc2Uk9QUlVBUCQlQCN0PWcu");
            IWebElement query = driver.FindElement(By.Id("QuestionId_rbf8060578cb4410ba72d3e97840ebd95"));
            query.SendKeys("David Luis");

next change:

IWebElement query = driver.FindElement(By.Name("Full Name"));

The results show : OpenQA.Selenium.ElementNotInteractableException: 'element not interactable (Session info: chrome=91.0.4472.77)'

Question: please guide me in this case should be used by class or by id, by Name

Upvotes: 0

Views: 207

Answers (2)

Qu&#226;n N&#232;
Qu&#226;n N&#232;

Reputation: 7

I have tried with "xpath" successfully. It's simple to understand. Below is the code I used for the project:

 var driverService = ChromeDriverService.CreateDefaultService();
            driverService.HideCommandPromptWindow = true;
            ChromeOptions options = new ChromeOptions();
            var driver = new ChromeDriver(driverService, options);
            driver.Navigate().GoToUrl("https://forms.office.com/Pages/ResponsePage.aspx?id=IWuHvDTxEkyiZY7Sa38PO_KiPmlYCpVOuIi4NlZfBs1UOVlCNlNFUTkyR0IwQVZaVDc2Uk9QUlVBUCQlQCN0PWcu");
            IWebElement query = driver.FindElement(By.XPath("//*[@id=\"form-container\"]/div/div/div/div/div[1]/div[2]/div[2]/div[2]/div[1]/div/div[2]/div/div/input"));
            query.SendKeys("David Luis");

Thanks for support.

Upvotes: 0

3r1c
3r1c

Reputation: 396

IWebElement query = driver.FindElement(By.Name("Full Name"));

By.name locator in Selenium is used to identify the elements of a webpage. This attribute can be mentioned as part of multiple tags like < input >, < button >, < select > etc. Unlike ID, this may or may not be unique to a page. A webpage may contain multiple tags with the same By.name attribute value. In such a case, if your intent is to select your desired element, the By.name locator in Selenium may not be the correct choice.

IWebElement query = driver.FindElement(By.Id("QuestionId_rbf8060578cb4410ba72d3e97840ebd95"));

is also the wrong choice because aria-labledby is not the Id where this is locking for.

so there are many possibilities:

f.e. you can give the element an unique id like:

<input id="uniqueInputID" class="office-form-question-textbox office-form-textfield-input form-control office-form-theme-focus-border border-no-radius" placeholder="Enter your answer" spellcheck="true" maxlength="4000" aria-labelledby="QuestionId_rbf8060578cb4410ba72d3e97840ebd95">
IWebElement query = driver.FindElement(By.Id("uniqueInputID"));

you could also use xpath:

IWebElement query = driver.findElement(By.XPath("//*[@id="form-container"]/div/div/div/div/div[1]/div[2]/div[2]/div[2]/div[1]/div/div[2]/div/div/input")

...

you can look on a page like this to find best practices: https://screenster.io/selenium-locators-best-practices/

Upvotes: 1

Related Questions