Selenium Automation unable to enter username in Wa

2019-09-18 16:41发布

I am trying to do registration for this site

Registration page is inside a popup page.

HTML Code:

<fieldset>
<label>Username:</label>
<input name="username" required="" type="text"/>
</fieldset>

When I try to find the element using below tried code, element is not getting find.

driver.FindElement(By.XPath(".//*[@id='load_form']/fieldset[1]/input")).SendKeys("Kal");

I have also tried this with using CssSelector, but facing the same issue.

driver.FindElement(By.CssSelector("div#load_box form#load_form input[name=username]")).SendKeys("kal");

When I execute above code, I have got an error like element not visible

Can anyone help me on this issue?

6条回答
女痞
2楼-- · 2019-09-18 16:48

Ajax popup on way2automation site is a tricky one because if you look for the username field by name By.name("username"), you will end up with 2 elements - one for username from signup popup, one from singin popup. To avoid this you have to explicity mention the correct element. This can be done via the following code:

    webDriver.get("http://way2automation.com/way2auto_jquery/index.php");
    WebDriverWait wait = new WebDriverWait(webDriver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href='#login'"))).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".ajaxlogin input[name='username']"))).sendKeys("my_username");

As you can see in the code I am using class of the login popup - .ajaxlogin. I have used Java, but the concept is the same - you have to refer to the username element via css selector with popup class included: By.cssSelector(".ajaxlogin input[name='username']")

查看更多
一纸荒年 Trace。
3楼-- · 2019-09-18 16:56

Try this below code using xpath locator

driver.FindElement(By.XPath("//input[@name='name']")).SendKeys("Kal");

Explanation of xpath:- Use name attribute of <input> tag.

Suggesstion:- Instead of using absolute xpath, use relative xpath.

Note:- Before reach to this web-element provide some few seconds of wait, so your driver may able to find the web-element. So, you will not get an error like element not visible

查看更多
Viruses.
4楼-- · 2019-09-18 16:59

The problem is that there are two username INPUT fields. The way I typically handle this is to find a parent of the element that I want that has an ID or something unique that will distinguish the two elements. In this case, you can use a simple CSS selector,

#load_box input[name='username']

Note the load_box ID that distinguishes the two INPUTs.

查看更多
相关推荐>>
5楼-- · 2019-09-18 17:06

that site has 2 forms with the id load_form so you're getting the first one which isn't visible since it's the login form. You want the second one which is the register form.

you can use a selector to grab one of the fields that exists on the registration page and then move up to it's parent form and get all descendants that are fieldsets to fill out.

查看更多
别忘想泡老子
6楼-- · 2019-09-18 17:07

Here is the xpath you can use to pass the text "Dev" into the field labelled with "Name".

driver.findElement(By.xpath("//div[@class='fancybox-overlay fancybox-overlay-fixed']//form[@id='load_form']/fieldset/input[@name='name']")).sendKeys("Dev");

Let me know if this answers your question.

查看更多
女痞
7楼-- · 2019-09-18 17:13

Use below xpath:

//*[@id='load_form']/fieldset[6]/input[@name='username']
查看更多
登录 后发表回答