我在运营商下拉列表中遇到了元素不可见的异常。我尝试了隐式等待,显式等待以及所有id,class,XPath,CSS选择器方式来查找元素。请帮助我获得正确的XPath。在选择运营商下拉列表时出现“元素不可见”异常错误消息。谢谢。
<select class="form-control error" id="smsCarrier" name="smsCarrier" required="" data-required-message="Carrier is required." aria-required="true" aria-invalid="false">
<option value="-1">Select a Carrier</option>
<option value="@message.alltel.com">Alltel</option>
<option value="@txt.att.net">AT&T</option>
<option value="@myboostmobile.com">Boost Mobile</option>
<option value="@sms.cricketwireless.net">Cricket Wireless</option>
<option value="@msg.fi.google.com">Project Fi</option>
<option value="@text.republicwireless.com">Republic Wireless</option>
<option value="@messaging.sprintpcs.com">Sprint</option>
<option value="@tmomail.net">T-Mobile</option>
<option value="@email.uscc.net">US Cellular</option>
<select class="form-control error" id="smsCarrier" name="smsCarrier" required="" data-required-message="Carrier is required." aria-required="true" aria-invalid="false">
<option value="-1">Select a Carrier</option>
<option value="@message.alltel.com">Alltel</option>
<option value="@txt.att.net">AT&T</option>
<option value="@myboostmobile.com">Boost Mobile</option>
<option value="@sms.cricketwireless.net">Cricket Wireless</option>
<option value="@msg.fi.google.com">Project Fi</option>
<option value="@text.republicwireless.com">Republic Wireless</option>
<option value="@messaging.sprintpcs.com">Sprint</option>
<option value="@tmomail.net">T-Mobile</option>
<option value="@email.uscc.net">US Cellular</option>
<option value="@vtext.com">Verizon</option>
<option value="@vmobl.com">Virgin Mobile</option>
</select>
I used Fluent wait.
public void ContinueWhenReady(By locator, int timeout)
{
for (int i = 0; i < 2; i++)
{
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(locator));
break;
}
catch (Exception e)
{
}
}
}
//PhoneNumberDetails details = table.CreateInstance<PhoneNumberDetails>();
ContinueWhenReady(By.CssSelector("#smsCarrier"), 50);
var SelectaCarrierDropDown = driver.FindElement(By.CssSelector("#smsCarrier"));
SelectaCarrierDropDown.Click();
var selectaCarrierElement = new SelectElement(SelectaCarrierDropDown);
//selectaCarrierElement.SelectByText(details.SelectaCarrier);
selectaCarrierElement.SelectByValue("@myboostmobile.com");
答案 0 :(得分:1)
在JAVA中,您可以执行以下操作:
WebDriverWait wait = new WebDriverWait(driver, 10);
Select dropdown = new Select(wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("smsCarrier")))));
dropdown.selectByVisibleText("Cricket Wireless");
答案 1 :(得分:0)
正如其他人提到的那样,您的问题还不够详细,不足以知道您的确切问题,但是我可以指出在处理Selenium中的下拉列表时有一些常见的问题。
首先,您需要确保选择实际上已加载到页面上,然后再尝试查找它。因此,您可能需要使用WebDriverWait来做到这一点。示例:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("smsCarrier")));
然后,您可以尝试找到下拉列表并将其存储在WebElement中:
WebElement carrierDropdown = driver.findElement(By.id("smsCarrier"));
诀窍是,要使用下拉菜单,您需要将该WebElement包装在Select对象中:
Select carrierSelect = new Select(carrierDropdown);
从那里,您可以使用该Select对象上的方法来操作它,例如从下拉列表中选择一个选项。示例:
carrierSelect.selectByVisibleText("Boost Mobile");
或
carrierSelect.selectByValue("@myboostmobile.com");
希望有帮助!