Selenium: handling multiple inputs with same xpath

纵然是瞬间 提交于 2019-12-11 12:04:52

问题


I am testing a website where the user has an option to change the input fields to Textbox and Dropdown. Whether the user changes it to dropdown or textbox, the xpath for the input field is the same. So, I am not sure how to write the test steps for this situation.

I would like to do something like:

if (dropdown present)
{
   Selenium.Select("dropdown xpath", "option");
}

else if (textbox present)
{
   Selenium.Type("texbox xpath", "option");
}

Can anyone please help me out? Is there a way in Selenium to check if a input is of type "text"?


回答1:


You can use selenium.isElementPresent(locator), enclosed within you if (..) construct to check for the presence of an element, and perform operations accordingly.




回答2:


I didn't see a simple property in selenium, but you can definitely get it by calling javascript:

IWebElement element = ...

string tagName = ((IJavaScriptExecutor)webDriver).ExecuteScript("return arguments[0].tagName", element).ToString();

Another alternative is to create 2 xpaths that will only work for one or the other, and attempt to find the textbox within a try/catch. If it hits the catch, then search for the dropdown instead.



来源:https://stackoverflow.com/questions/8272500/selenium-handling-multiple-inputs-with-same-xpath

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