[英]Selenium / C# WebDriverWait Not Waiting
我有一个网站,上面有一个jQuery幻灯片,其顶部是我想使用的所有语言的列表: http : //testing.bestshippers.com/net/index.aspx ,滑块是“语言选择”按钮最佳。
我可以单击它,但是在单击幻灯片中的元素时出现错误。 我相信这是因为在尝试选择它们之前,我需要暂停几秒钟。 可能是错的吗? 我相对较新,但是我已经阅读了有关WebDriverWait和改变焦点的各种内容。
//check spanish
driver.FindElement(By.XPath("//*[@id='openCloseWrap']/img")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
driver.FindElement(By.Id("ButtonSPFlag")).Click();
String check = driver.FindElement(By.XPath("html/body/form/div[5]/div/div[1]/div/p")).Text;
Console.Out.WriteLine(check);
上面的代码单击了openCloseWrap(语言选择按钮),然后我试图暂停几秒钟(100),然后尝试单击SP标志来更改语言。
任何人都可以提供帮助,为什么我的等待不会暂停?
您正在启动等待,但不等待任何东西。 您可能想做类似的事情:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
// wait.Until(By.Id("ButtonSPFlag"));
IWebElement element = wait.Until(driver => driver.FindElement(By.Id("ButtonSPFlag")));
element.Click();
另外,您可以设置隐式等待,但是我会选择第一个。
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
我都赞成你们,但我的代表点还不够。
我为此工作了4天,问题似乎与语言栏的显示方式有关。 我似乎无法通过CSS,XPath或命名来引用任何内容。 我现在将继续进行我的项目。 我可以检查以确保它存在,并且可以通过(请参阅下文),但是实际的交互由于某种原因而失败。 我将继续工作,并且我将重新审视。 感谢您的时间和精力!
DoesElementExistX(driver, sw, "//*[@id='openCloseWrap']/img");
DoesElementExistX(driver, sw, "//*[@id='ButtonUSFlag']");
DoesElementExistX(driver, sw, "//*[@id='ButtoFRFlag']");
DoesElementExistX(driver, sw, "//*[@id='ImageButton1']");
DoesElementExistX(driver, sw, "//*[@id='ButtonDEFlag']");
DoesElementExistX(driver, sw, "//*[@id='ButtonITFlag']");
DoesElementExistX(driver, sw, "//*[@id='ButtonSPFlag']");
#region[DoesElementExistX]
public static void DoesElementExistX(IWebDriver driver, StreamWriter sw, String id)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(2));
try
{
wait.Until(ExpectedConditions.ElementExists(By.XPath(id)));
}
catch (WebDriverTimeoutException)
{
sw.WriteLine("FAILED - " + id);
}
}
#endregion
我进一步研究了这一点。
您尝试通过id
单击Language元素。 这是一个问题,因为Selenium将单击元素的中心(除非另有说明),并且元素的中心位于“语言”区域的左侧。
我认为这将满足您的需求:
driver.FindElement(By.CssSelector(".topMenuAction")).Click();
driver.FindElement(By.Id("ButtonSPFlag")).Click();
String Check = driver.FindElement(By.CssSelector("#loginBoxInner>p")).Text;
您可能需要添加Sleep()
或使用WebDriverWait()
。
可能是语言选择栏始终位于DOM中,并且当您单击“展开”时,它只是显示html而不是创建或注入html?
如果是这样,那么您的“等待”将立即返回,因为您说的是“等待直到元素”在HTML中,并且在您的情况下,它始终存在。
您需要使等待更加智能,例如“等待直到显示”,甚至可能是“等待直到显示并静止”,因为您还需要等待动画完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.