问题
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