Selenium Automation unable to enter username in Way2Automation website

风格不统一 提交于 2019-12-25 05:05:34

问题


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?


回答1:


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




回答2:


Use below xpath:

//*[@id='load_form']/fieldset[6]/input[@name='username']



回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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']")



来源:https://stackoverflow.com/questions/43634899/selenium-automation-unable-to-enter-username-in-way2automation-website

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