我的网站顶部有一个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标志来改变语言。
任何人都可以提供任何帮助,说明为什么我的等待不会暂停?
答案 0 :(得分:3)
你正在开始等待,但不是在等待任何事情。你可能想做类似的事情:
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));
答案 1 :(得分:0)
我进一步研究了这个。
您试图通过id
点击语言元素。这是一个问题,因为Selenium会点击元素的中心(除非另有说明),元素的中心位于语言区域的左侧。
我认为这将满足您的需求:
driver.FindElement(By.CssSelector(".topMenuAction")).Click();
driver.FindElement(By.Id("ButtonSPFlag")).Click();
String Check = driver.FindElement(By.CssSelector("#loginBoxInner>p")).Text;
您可能需要添加Sleep()
,或使用WebDriverWait()
。
答案 2 :(得分:0)
是不是语言选择栏总是在DOM中,当你点击“展开”时,它只是显示html而不是创建或注入它?
如果是这样,那么你的“等待”将立即返回,因为你说“等到元素”在HTML中,并且在你的情况下,它总是在那里。
你需要让你的等待更聪明,例如“等到显示”或者甚至“等到显示和静止”,因为你还需要等待动画完成
答案 3 :(得分:0)
我会向你们两个投票,但我还没有足够的重复点。
我在这工作了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