Ahmed Habiw
Ahmed Habiw

Reputation: 31

Write someting in a <input> tag in html with selenium c# doesn't work

I wanted to fill in a input from website with html.

THis how the part of the website (https://account.protonmail.com/signup?language=de) looks like:

<div class="flex-item-fluid">
<input autocomplete="username" autocapitalize="off" autocorrect="off" spellcheck="false" aria-invalid="false" id="username" aria-describedby="id-1" class="w100 inputform-field" value="">
</div>

This my c# selenium part:

var elem = driver.FindElement(By.XPath("//*[@id='username']")).SendKeys("test");
// I also tried this:
var elem = driver.FindElement(By.XPath("//input[@id='username']")).SendKeys("test");

I hope somebody can help me.

Upvotes: 3

Views: 223

Answers (3)

Nandan A
Nandan A

Reputation: 2922

The element you are looking resides in an iframe so first switch the frame.

xPath for iframe:

//iframe[contains(@src,'Name=username')]

Then find the element using below xPath,

//input[@id='username']

Completed code:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'Name=username')]")));

var elem = driver.FindElement(By.XPath("//*[@id='username']")).SendKeys("test");

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

The username field is in iframe

use the below xpath to switch to iframe first then you can continue with the rest of the code:

//iframe[@src]

something like this :

IWebElement iframe = driver.FindElement(By.Xpath("//iframe[@src]"));
driver.SwitchTo().Frame(iframe);

Also, I see multiple elements for username field. Always remember we should give more precedence to css_selector over xpath

Username field can be locate with this css:

body.color-norm.bg-norm input[id='username']

so in code something like this :

var elem = driver.FindElement(By.CssSelector("body.color-norm.bg-norm input[id='username']")).SendKeys("test");
var pass = driver.FindElement(By.CssSelector("input[id='password']")).SendKeys("password here");

Upvotes: 1

Emin Niftiyev
Emin Niftiyev

Reputation: 233

you can try this

var elem = driver.FindElement(By.CssSelector(".w100.inputform-field")).SendKeys("")

Here we search by class name, not by input name. Good Luck.

if it worked for you. USEFUL

Upvotes: 0

Related Questions