我正在使用Selenium来清除Salesforce账户对象的现有送货地址字段,并分配新的值。我正在用C#编码,并在Visual Studio 2019上运行。即使我有ImplicitWaits甚至SeleniumExtras.WaitHelpers.ExpectedConditions调用(C# Selenium "ExpectedConditions is obsolete"。)我得到的情况是,文本区域或文本框没有被完全填充。
我的代码如下。我怎样才能解决这个问题?
private string shippingStreet = "56789 Liberty Street"; // 80 character limit
private string shippingCity = "Toronto"; // 40 character limit
private string shippingState = "ON"; // 80 character limit
private string shippingZip = "87654"; // 20 character limit
private string shippingCountry = "Canada"; // 80 character limit
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(50));
IWebElement shStreet = driver.FindElement(By.XPath(...));
shStreet.Clear();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
shStreet.SendKeys(shippingStreet);
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.TextToBePresentInElementValue(shStreet, shippingStreet));
IWebElement shCity = driver.FindElement(By.XPath(...));
shCity.Clear();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
shCity.SendKeys(shippingCity);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.TextToBePresentInElementValue(shCity, shippingCity));
// similar code for state, zip and country
你可以试试这个方法。
只要调用它并添加xpath: WaitForElementDisplayed_byXPathTime("/myPath")。
WaitForElementDisplayed_byXPathTime
public static void WaitForElementDisplayed_byXPathTime(string value)
{
var wait = new WebDriverWait(Driver, new TimeSpan(0, 0, 30));
wait.Until(webDriver => webDriver.FindElement(By.XPath(value)).Displayed);
}
我在这些方法上做的另一件事是为单个字符创建一个新的类型方法,就像在手机上一样。这只是让它慢了一点。
public static void TypeCharsIndividually(IWebElement element, string expectedValue)
{
//use your code for element displayed and element enabled
element.Click();
element.Clear();
foreach (char c in expectedValue)
{
element.SendKeys(c.ToString());
Thread.Sleep(100);
}
}
java点击
public static void ClickJava(IWebElement element)
{
IJavaScriptExecutor executor = driver IJavaScriptExecutor;
executor.ExecuteScript("arguments[0].click();", element);
}
WaitForElement
public static bool WaitForElementDisplayed_byXPath(string path)
{
var result = true;
try { _wait.Until(webDriver => driver.FindElement(By.XPath(path)).Displayed); }
catch (StaleElementReferenceException) { WaitForElementDisplayed_byXPath(path); }
catch (NoSuchElementException) { WaitForElementDisplayed_byXPath(path); }
catch (WebDriverTimeoutException) { result = false; }
return result;
}