Selenium driver.Title返回不同​​的值C#

时间:2017-01-09 02:25:17

标签: c# selenium selenium-webdriver webdriver selenium-firefoxdriver

在我的程序中,我让驱动程序单击进入页面并获取其标题并将其放入字符串中。在调试中我看到driver.Title是新页面的标题,但该字符串包含上一页的标题。如何修复此问题并将标题为第二页的文件分配给字符串?

//Title == "Supreme"
driver.Navigate().GoToUrl(baseURL + "/shop/all");
driver.FindElement(By.XPath(".//*[@id='container']/article[" + num.ToString() + "]/div/a/img")).Click();
//title of this page should be Supreme: Supreme®/Schott® Shearling Bomber - Lime Green

string title = driver.Title;
//returns first title and not title of new page

1 个答案:

答案 0 :(得分:0)

这可能是计时问题,这就是为什么你在慢速调试中看到正确的值而不是在运行脚本时。您可以使用ExpectedConditions TitleIsTitleContains进行明确等待以等待正确的标题

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.TitleIs("Supreme: Supreme®/Schott® Shearling Bomber - Lime Green"));
// or
wait.Until(ExpectedConditions.TitleContains("Shearling Bomber - Lime Green"));

对于您提供的条件,这将等待最多10秒。如果标题已更改,则会立即继续,否则您将获得TimeoutException